Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Sybase stored procedure -- getting last months date

This is my first question ever on any forum.

I am working on a Sybase procedure where I need to get last month's date {as MMddyyyy} to filter records against a date field. The way I am currently doing it seems to be rather patchy though it is working.

CREATE PROC sy_previous_month_report AS
BEGIN
DECLARE @month CHAR(3) ,
@year CHAR(4) ,
@date CHAR(12)

SELECT @month = substring(convert(char(20), dateadd(mm, -1, getdate()), 106), 4, 3)
SELECT @year = substring(convert(char(20), getdate(), 106), 8, 4)
IF @month = "Dec"
SELECT @year = substring(convert(char(20), dateadd(yy, -1, getdate()), 106), 8, 4)

SELECT @date = @month+" "+@year+"%"
...

Is there a better way of doing this?

TIA! :)
[858 byte] By [aniseed] at [2007-11-11 7:12:14]
# 1 Re: Sybase stored procedure -- getting last months date
I guess I should have done a little search first. I managed to reduce that part of the code down to this... eliminates a few checks. :)

CREATE PROC test_proc AS
BEGIN
DECLARE @dtstr VARCHAR(20),
@month CHAR(3),
@year CHAR(4)

SELECT @dtstr = convert(char(20), dateadd(mm, -2, getdate()))
SELECT @month = substring(@dtstr, 1, 3)
SELECT @year = substring(@dtstr, 8, 4)

SELECT @dtstr = @month +"%" +@year +"%"
SELECT COUNT(*) FROM UserTable WHERE CreatedDate LIKE @dtstr

END

If anyone could suggest something simpler...
aniseed at 2007-11-11 23:47:53 >