Convert date and time to number in year and year

I have a datetime, for example. 2014-04-24 00: 28: 53.897 and you want to convert this into the selected query for the month by number and year - 2014 4.

+4
source share
7 answers

try it

SELECT FORMAT(GETDATE(),'MM yyyy')

replace getdate with datetime column

I hope he helps you.

+6
source

Use DATEPART

SELECT CONCAT(DATEPART(mm,datefield),' ',DATEPART(yyyy,datefield)) AS monthyear
FROM yourtable

SQL FIDDLE: http://sqlfiddle.com/#!6/9c896/7/0

+3
source

- : ( GETDATE() date/datetime)

SELECT CAST(DATEPART(MONTH,GETDATE()) AS VARCHAR(2)) + ' ' + CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR(4))
+2

SQL Server 2012 ( , CONCAT):

SELECT CONCAT(DATEPART(mm,dateField),' ',DATEPART(yyyy,dateField))
AS MONTH_YEAR
FROM TABLENAME;
+2

. declare @d datetime = '2014-04-24 00: 28: 53.897' FORMAT (@d, 'MM yyyy')

.

+2
source
/* sql code */
--if you have datetime as text use this to convert to date
--select cast('2014-04-24 00:28:53.897' as date)
select convert(varchar(2), month(cast('2014-04-24 00:28:53.897' as date))) + ' ' + convert(char(4), year(cast('2014-04-24 00:28:53.897' as date)))

--if you have datetime field already use this
select convert(varchar(2), month(getdate())) + ' ' + convert(char(4), year(getdate())) 
+2
source

Use DATEPART To Do This

SELECT Convert(VARCHAR(10),DATEPART(mm,yourfield),111) + '  ' + 
Convert(VARCHAR(10),DATEPART(yyyy,yourfield),111) AS outputmonthyear
FROM yourtableName

http://sqlfiddle.com/#!6/fa887/7

DATEPART Definition and Use

The DATEPART () function is used to return one part of a date / time, for example, year, month, day, hour, minute, etc.

Syntax DATEPART (datepart, date) Where date is a valid date expression.

For more information, visit

http://www.w3schools.com/sql/func_datepart.asp

+1
source

All Articles