snippetsqlMajor
Convert date yyyy-mm-dd to integer YYYYMM
Viewed 0 times
yyyymmconvertyyyydateinteger
Problem
How can I convert @dateb:
that returns
Thanks
SET @dateb = dateadd(month, datediff(month, 0, getdate()) - 3, 0)that returns
2014-04-04 as date to an integer of 201404Thanks
Solution
On version 2012 or higher you can use the
On versions prior to 2012 you can do the formatting with the
format function to get just year and month, then cast it as an int.On versions prior to 2012 you can do the formatting with the
convert function, then cast as int.declare @dateb datetime
set @dateb = getdate()
select cast(format(@dateb,'yyyyMM') as int) --2012 or higher
select cast(convert(varchar(6),@dateb,112) as int) -- all versionsCode Snippets
declare @dateb datetime
set @dateb = getdate()
select cast(format(@dateb,'yyyyMM') as int) --2012 or higher
select cast(convert(varchar(6),@dateb,112) as int) -- all versionsContext
StackExchange Database Administrators Q#106898, answer score: 22
Revisions (0)
No revisions yet.