Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query - Can I compare using LEN in SELECT clause?

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?

like image 466
Matthew Jones Avatar asked Feb 15 '26 14:02

Matthew Jones


2 Answers

SELECT  HasComments = CASE WHEN LEN(Comments) > 1 THEN 1 ELSE 0 END
FROM    TableName
like image 116
Quassnoi Avatar answered Feb 18 '26 17:02

Quassnoi


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.

like image 44
n8wrl Avatar answered Feb 18 '26 16:02

n8wrl