Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using operators in Case statement

Author  Topic 

kiddoOnSQL
Starting Member

16 Posts

Posted - 2010-06-01 : 02:32:19
I am creating a view, where one of the columns needs to display a boolean value of true or false based on certain comparisons. For this comparison I decided to use the Case statment. Code excerpt is as follows:

CREATE VIEW dbo.view1
AS

SELECT column 1
,Case DATEDIFF(dd,B.Finishdate,GETDATE()) when <0 then 1 else 0 end column2
,column 3
FROM dbo.xyz B

The intent is to compare the value in the FinishDate column with the current date to determine if it is in the future or not. Using the 'when<0' expression assumes that DateDiff will return a negative value if Finishdate is in the future.
However I get the error 'Incorrect syntax near '<' when I try to create the view.

How else can I compare the date value?

matty
Posting Yak Master

161 Posts

Posted - 2010-06-01 : 02:34:56
SELECT column 1
,Case WHEN DATEDIFF(dd,B.Finishdate,GETDATE()) < 0 then 1 else 0 end column2
,column 3
FROM dbo.xyz B
Go to Top of Page
   

- Advertisement -