将日期分为月份&一年明智 [英] Split date into month & year wise

查看:78
本文介绍了将日期分为月份&一年明智的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的sql server中有一个这样的Date列。



ex :jan-2013,feb-2013 ....



现在我想把它分成月份和年份明智



month ---- jan-1,year --- 2013

Hi,

I have a Date column like this in my sql server.

ex: jan-2013, feb-2013....

now i want to split that into month & year wise

month ----jan-1, year---2013

推荐答案

使用功能:

[ ^ ]

YEAR [ ^ ]

DATEPART [ ^ ]
Use functions:
MONTH[^]
YEAR[^]
DATEPART[^]


您好,



尝试以下查询。

Hi,

try below query.
SELECT DATEPART(MONTH, DateColumn) 'Month', DATEPART(YEAR, DateColumn) 'Year' FROM YourTable



更多关于 DatePart 请参阅DATEPART-MSDN [ ^ ]



希望它有所帮助。


more on DatePart refer DATEPART-MSDN[^]

hope it helps.


试试这个

try this
declare @date varchar(max)
set @date='jan-2013'
select right(@date,4) as Year, 
	substring(@date,0,len(@date)-4) as MonthName,
	datepart(month,'1900-' + substring(@date,0,len(@date)-4) + '01' ) as MonthNumber



快乐编码!

:)


Happy Coding!
:)


这篇关于将日期分为月份&一年明智的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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