views:

60

answers:

4

I basically want to do this:

SELECT HasComments = CASE (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END FROM TableName

In other words, return a boolean telling me whether the length of Comments is greater than 1. This gives me a syntax error.

How can I accomplish this?

+4  A: 
SELECT  HasComments = CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END
FROM    TableName
Quassnoi
So I had the compare in the wrong place. Gotcha. Thanks @Quassnoi!
Matthew Jones
+1  A: 

A better way would be to make Comments NULLable and check for that. Indexes could then be leveraged instead of the table-scan LEN() will cause.

n8wrl
A: 

you're missing the when and end

SELECT HasComments = CASE WHEN (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END
 FROM TableName
Glennular
A: 

Since you have no WHERE clause, you're most likely returning a column of data:

SELECT  CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END as 'HasComments'
FROM    TableName 
BoltBait