I have a stored procedure using Convert where the exact same Convert
string works in the SELECT portion of the procedure but fails in the
WHERE portion.
The entire SP is listed below.
Specifically, I have a problem with this portion in the WHERE clause:
DATEADD(Day,tbl MyEventTableNam e.ReminderDays, @DateNow) Between
CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)+1) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) AND
tblMyEventTable Name.Recurrence End)
(tblMyEventTabl eName.ReminderD ays = days ahead the user wants to be
reminded)
If I use DATEPART(Month, @DateNow)+1
it fails even though I use this in the SELECT portion. The error
mesage is "The conversion of char data type to smalldatetime data type
resulted in an out-of range smalldatetime value."
If I use DATEPART(Month, @DateNow)
it doesn't fail.
If I use DATEPART(Month, @DateNow)+2
it doesn't fail.
If I use DATEPART(Month, @DateNow)+3
it fails with error message "Error converting data type datetime to
smalldatetime."
If I use DATEPART(Month, @DateNow)+4
it doesn't fail.
What the SP is trying to do is to evaluate a series of dates in a
table of personal reminders. If the user has set the reminder to
"monthly", the SP evaluates if the day of the month has already passed
today's date, it so, it creates a reminder date (myReminderDate ) with
next month's day of the month.
(RecurrenceEnd is the date the reminder is set to stop):
Alter Procedure SPExample
@DateNow smalldatetime
As
SELECT
CASE WHEN tblMyEventTable Name.Recurrence Pattern ='monthly' AND
DATEPART(d, @DateNow) <= DATEPART(d,tblM yEventTableName .TaskDateTime)
and tblMyEventTable Name.Recurrence End > @DateNow
/*monthly event: today day is less than or equal to than monthly task
month-day so remind this month*/
THEN CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) ELSE
CASE WHEN tblMyEventTable Name.Recurrence Pattern ='monthly' AND
DATEPART(d, @DateNow) > DATEPART(d,tblM yEventTableName .TaskDateTime)
and tblMyEventTable Name.Recurrence End > @DateNow
/*monthly event: today day is greater than monthly task month-day so
remind next month*/
THEN CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)+1) + '/'
+ str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) ELSE
/*RecurrencePatt ern is not set to monthly so just use the reminder
date*/
tblMyEventTable Name.TaskDateTi me
END
END
AS myReminderDate, tblMyEventTable Name.myTaskName
FROM
tblMyEventTable Name
WHERE
/* takes care of monthly events that are after or equal today's day of
year */
tblMyEventTable Name.Recurrence Pattern ='monthly'
AND
DATEPART(d, @DateNow) <= DATEPART(d,tblM yEventTableName .TaskDateTime)
AND
tblMyEventTable Name.Recurrence End > @DateNow
AND
DATEADD(Day,tbl MyEventTableNam e.ReminderDays, @DateNow) Between
CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) AND
tblMyEventTable Name.Recurrence End)
OR
/* takes care of monthly events that are before today's day of year */
tblMyEventTable Name.Recurrence Pattern ='monthly'
AND
DATEPART(d, @DateNow) > DATEPART(d,tblM yEventTableName .TaskDateTime)
AND
tblMyEventTable Name.Recurrence End > @DateNow
AND
DATEADD(Day,tbl MyEventTableNam e.ReminderDays, @DateNow) Between
CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)+1) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) AND
tblMyEventTable Name.Recurrence End)
string works in the SELECT portion of the procedure but fails in the
WHERE portion.
The entire SP is listed below.
Specifically, I have a problem with this portion in the WHERE clause:
DATEADD(Day,tbl MyEventTableNam e.ReminderDays, @DateNow) Between
CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)+1) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) AND
tblMyEventTable Name.Recurrence End)
(tblMyEventTabl eName.ReminderD ays = days ahead the user wants to be
reminded)
If I use DATEPART(Month, @DateNow)+1
it fails even though I use this in the SELECT portion. The error
mesage is "The conversion of char data type to smalldatetime data type
resulted in an out-of range smalldatetime value."
If I use DATEPART(Month, @DateNow)
it doesn't fail.
If I use DATEPART(Month, @DateNow)+2
it doesn't fail.
If I use DATEPART(Month, @DateNow)+3
it fails with error message "Error converting data type datetime to
smalldatetime."
If I use DATEPART(Month, @DateNow)+4
it doesn't fail.
What the SP is trying to do is to evaluate a series of dates in a
table of personal reminders. If the user has set the reminder to
"monthly", the SP evaluates if the day of the month has already passed
today's date, it so, it creates a reminder date (myReminderDate ) with
next month's day of the month.
(RecurrenceEnd is the date the reminder is set to stop):
Alter Procedure SPExample
@DateNow smalldatetime
As
SELECT
CASE WHEN tblMyEventTable Name.Recurrence Pattern ='monthly' AND
DATEPART(d, @DateNow) <= DATEPART(d,tblM yEventTableName .TaskDateTime)
and tblMyEventTable Name.Recurrence End > @DateNow
/*monthly event: today day is less than or equal to than monthly task
month-day so remind this month*/
THEN CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) ELSE
CASE WHEN tblMyEventTable Name.Recurrence Pattern ='monthly' AND
DATEPART(d, @DateNow) > DATEPART(d,tblM yEventTableName .TaskDateTime)
and tblMyEventTable Name.Recurrence End > @DateNow
/*monthly event: today day is greater than monthly task month-day so
remind next month*/
THEN CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)+1) + '/'
+ str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) ELSE
/*RecurrencePatt ern is not set to monthly so just use the reminder
date*/
tblMyEventTable Name.TaskDateTi me
END
END
AS myReminderDate, tblMyEventTable Name.myTaskName
FROM
tblMyEventTable Name
WHERE
/* takes care of monthly events that are after or equal today's day of
year */
tblMyEventTable Name.Recurrence Pattern ='monthly'
AND
DATEPART(d, @DateNow) <= DATEPART(d,tblM yEventTableName .TaskDateTime)
AND
tblMyEventTable Name.Recurrence End > @DateNow
AND
DATEADD(Day,tbl MyEventTableNam e.ReminderDays, @DateNow) Between
CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) AND
tblMyEventTable Name.Recurrence End)
OR
/* takes care of monthly events that are before today's day of year */
tblMyEventTable Name.Recurrence Pattern ='monthly'
AND
DATEPART(d, @DateNow) > DATEPART(d,tblM yEventTableName .TaskDateTime)
AND
tblMyEventTable Name.Recurrence End > @DateNow
AND
DATEADD(Day,tbl MyEventTableNam e.ReminderDays, @DateNow) Between
CONVERT(smallda tetime,str(DATE PART(Month, @DateNow)+1) + '/' +
str(DATEPART(Da y, tblMyEventTable Name.TaskDateTi me)) + '/' +
str(DATEPART(Ye ar, @DateNow)),101) AND
tblMyEventTable Name.Recurrence End)
Comment