Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Cast bool as integer

Why does the query:

SELECT CAST((column LIKE '%string%') AS INT)+100

return

Incorrect syntax near the keyword 'AS'

like image 374
Mansoor Avatar asked Feb 18 '23 10:02

Mansoor


1 Answers

Because bool is not a type in T-SQL. It does not exist. Boolean expressions are not of type bit. They don't have a type - they are only allowed if allowed by the grammar in special places. And yes, this is awful.

SELECT (case when (column LIKE '%string%') then 1 else 0 end)+100
like image 71
usr Avatar answered Feb 20 '23 01:02

usr