Search code examples
sybasesap-asesap-iq

get last day of previous year from sybase ase


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)

Solution

  • 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.