如何使用SQL日期函数更改日期,年份的格式 [英] How to Change Format of Date , Year using SQL Date Functions

查看:98
本文介绍了如何使用SQL日期函数更改日期,年份的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我这个

我在桌子上有一个栏目



栏目1

2014- 11-02 00:00:00.000

2015-11-02 00:00:00.000

2015-12-03 00:00:00.000



我想输出为



第1栏

NOV-2014

NOV-2015

2015年12月



如何编写SQL查询?

解决方案

  SELECT   LEFT (Datename(Month,Column1),  3 )+ '   - ' + CAST (YEAR(Column1) as   Varchar  FROM 您的 


这取决于您的列的数据类型。

如果它是VARCHAR或NVARCHAR,那么这是一个问题。如果它是DATETIME,那么它非常简单 - 但可能不可取。



如果你把它存储为基于字符串的列,那么你需要改变它并转换在某种程度上坏值进入DATETIME之前的数据。



即使作为DATETIME值,在SQL中执行此操作也不是一个好主意:它应该是在演示文稿软件中完成,而不是数据库。为什么?因为对于英语用户,你需要JAN,FEB,...但对于法国用户来说,它应该是JAN,FEV,......并且SQL服务器无法访问您的用户文化,与您的演示软件不同。



但是......在SQL中它很简单:

  SELECT  SUBSTRING( CONVERT  VARCHAR  11 ),MyDateColumn, 6 ), 4  6 


Can anyone help me on this
I Have a Column in table

Column1
2014-11-02 00:00:00.000
2015-11-02 00:00:00.000
2015-12-03 00:00:00.000

I want Output as

Column1
NOV-2014
NOV-2015
Dec-2015

How to write a SQL Query?

解决方案

SELECT LEFT(Datename(Month, Column1),3) + '-' + CAST(YEAR(Column1) as Varchar) FROM Your Table


It depends on what datatype your column is.
If it's VARCHAR or NVARCHAR, then it's a problem. If it's DATETIME, then it's pretty simple - but probably not advisable.

If you have it stored as a string based column, then you need to change that and convert the data to DATETIME before somehow a "bad" value gets in there.

Even as a DATETIME value, it's not a good idea to do this in SQL: it should be done in your presentation software, not the DB. Why? Because for an English user you need JAN, FEB, ... but for a French user it should be JAN, FEV, ... and SQL server doesn't have access to your user culture, unlike your presentation software.

But...in SQL it's pretty easy:

SELECT SUBSTRING(CONVERT(VARCHAR(11), MyDateColumn, 6), 4, 6)


这篇关于如何使用SQL日期函数更改日期,年份的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆