Reputation: 603
I need to set a where condition to the last date of previous year. I looking for the solution using dateadd function but i can not figure it out. This code gives me the last day of current month. But how to get the last day of december last year '2015-12-31' of have tried different ways but it all gives me odd results.
declare @today datetime
select @today=getdate()
select convert(varchar(10), dateadd(dd, -day(dateadd(mm, 1, @today)),dateadd(mm, 1, @today)),101)
Upvotes: 1
Views: 3608
Reputation: 1
SELECT DATEADD(DD,-1,DATEADD(YEAR, DATEDIFF(YEAR, '', GETDATE())+1, ''))
Upvotes: 0
Reputation: 160
select convert(datetime, convert(varchar, datepart(year, getdate()) - 1 ) + '/12' + '/31')
Dec 31 2015 12:00AM
Upvotes: 0
Reputation: 96
Assuming Sybase ASE, not ASA or IQ:
declare @lastyear smallint, @dec31 datetime
select @lastyear = datepart(year,getdate()) - 1
select @dec31 = convert(datetime, convert(char(4), @lastyear) + "1231")
select @dec31
This produces
--------------------------
Dec 31 2015 12:00AM
@lastyear contains 2015, the last year, then text "2015" is concatenaded into "20151231", which is the desired date in AAAAMMDD char format. Next step converts it into date, datetime or smalldatetime. @dec31 stores that result.
Upvotes: 2