Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is null

Status
Not open for further replies.

cards4

Technical User
Dec 9, 2004
38
US
Hello,
I am using a case statement to determine if a date is null or not. Here is my code:
SELECT *,
length_of_service = case rehire_date
when rehire_date is null THEN
round(convert(decimal, datediff(dd, hire_date, getDate()))/365.2422,2)
ELSE
round(convert(decimal, datediff(dd, rehire_date, getDate()))/365.2422,2)
END,
However, I'm getting "incorrect syntax near the keyword 'is'" error. I just want to see if the rehire date is null or not. I'm not entirely sure what I'm doing wrong. Please help! Thank you.
 
Try it this way.

Code:
SELECT *, 
length_of_service = case [!]When rehire_date Is NULL [/!]
Then round(convert(decimal, datediff(dd, hire_date, getDate()))/365.2422,2)
ELSE round(convert(decimal, datediff(dd, rehire_date, getDate()))/365.2422,2)
END

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I think you want your case statement to read like this:

Code:
case rehire_date
[b]when null[/b] THEN
round(convert(decimal, datediff(dd, hire_date, getDate()))/365.2422,2)
ELSE
round(convert(decimal, datediff(dd, rehire_date, getDate()))/365.2422,2)
END

You are kind of mixing the two acceptable syntaxes for case statements there.

Hope it helps,

Aelx

A wise man once said
"The only thing normal about database guys is their tables".
 
George I am stalking you this morning :p

A wise man once said
"The only thing normal about database guys is their tables".
 
Alex,

Your syntax doesn't work because you cannot compare the value to null that way. Check it out.

Code:
Declare @Temp Table(Data int)

Insert Into @Temp Values(1)
Insert Into @Temp Values(NULL)

Select Case Data When Null Then 'unknown' Else 'known' end From @Temp

Select case when Data Is NULL Then 'unknown' Else 'known' end From @Temp

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Right you are George. Thanks for setting me straight.

Have a
star.gif


A wise man once said
"The only thing normal about database guys is their tables".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top