SQLServer - select bool if the column starts with a row

I would like to choose the boolean value of whether the column starts with a specific row.

SELECT (name LIKE 'foo%') AS isFoo FROM bar;

Is there any way to do this without using the built-in CASE?

+5
source share
6 answers

No

SQL Server does not have an implicit logical CAST, not a logical type

SELECT CAST(CASE WHEN name LIKE 'foo%' THEN 1 ELSE 0 END AS bit) AS isFoo
FROM bar;
+18
source

You may not need a throw depending on your use:

SELECT CAST(PATINDEX('foo%'), name) AS bit) FROM bar

It returns 1 if the col begins with the text otherwise 0. No CASE.

+4
source

A UNION CASE, . , 'foo%', , 'foo%'

- :

SELECT 1 AS [YourBoolean], 'fool' WHERE 'fool' LIKE 'foo%'
UNION
SELECT 0, 'fuel' WHERE 'fuel' NOT LIKE 'foo%' 
ORDER BY 1

( .)

+3

User Defined, inorder, , foo.

+2

gbn, , (, , )

SELECT CAST(CASE WHEN LEFT(name, 3)='foo' THEN 1 ELSE 0 END AS bit) AS isFoo
FROM bar;
+1
SELECT 
  CASE WHEN name LIKE 'foo%'          
     THEN 1
    ELSE  0
  END as isFoo

  FROM bar
0

All Articles