I prefer 1/0 instead of t/f, so what should I use when converting boolean to integer?
select coalesce((null::boolean)::int, 0)
OR
select case null::boolean when 't' then 1 else 0 end
... something else?
Regardless of which you do, a Boolean null does not equal false, any more than a numeric null equals zero.
Try:
Cast(col1 as integer)
If you really wanted to treat null as false then:
case when col1 then 1 else 0 end
It would be a Bad Thing though
Related
Below is what my data looks like for the case when statement:
0
1
NULL
Not Obtainable
NULL
Not Obtainable
2
0
NULL
1
NULL
NULL
Here is the query I am trying to use:
SELECT
*,
CASE
WHEN ER_MENTAL > 1 THEN 'Yes'
WHEN ER_MENTAL < 1 THEN 'No'
WHEN ER_MENTAL = 'Not Obtainable' THEN 'No'
END AS ER_MENTAL_RESP
FROM
#HLQ
This is the error I'm getting:
Conversion failed when converting the varchar value 'Not Obtainable' to data type int.
I've tried various version of CONVERT in front of each case when with no luck
If you need a version that will work on earlier versions of SQL Server then using ISNUMERIC should work:
select er_mental,
case when ISNUMERIC(er_mental) = 1 then
case when er_mental > 1 then 'Yes'
when er_mental < 1 then 'No'
end
when er_mental = 'Not obtainable' then 'No'
end as er_mental_resp
from #hlq;
Your column is VARCHAR so you can't compare it to an INT without converting it. You can use TRY_CONVERT since you are using 2012 which will avoid your error. You could also compare it to it's varchar equivalent, like ER_MENTA > '1' but you could run into issues since varchar's aren't compared the same way. i.e. 11 is < 2 when using varchar. select 1 where '11' < '2'
SELECT *,
CASE WHEN TRY_CONVERT(INT, ER_MENTAL) > 1 THEN 'Yes'
WHEN TRY_CONVERT(INT, ER_MENTAL) < 1 THEN 'No'
WHEN ER_MENTAL = 'Not Obtainable' THEN 'No'
END AS ER_MENTAL_RESP
FROM #HLQ
I have a SQL Server 2005 table which contains several columns which I need to check if they have data using
case when MyColumn <> '' then 1 else 0 END
My problem is that I need something to work on all datatypes, as the above wouldn't work on 0 values
example
case when MyColumn <> '' then 1 else 0 END
will return false if the value is 0, which I require otherwise.
I tried to trim, convert, cast, using len and datalength to no avail.
Anyone have any suggestion please?
Put check for 0 and null as well. some thing like:
case when MyColumn <> '' or MyColumn <>'0' Or MyColumn IS NOT NULL or MyColumn <>0 then 1 else 0 END
My assumption was that it would return a true if that value was numeric (within the isnumeric range) but FALSE if the ISNULL returns 'blah'. Seems like my assumption was off...
I'm using the it in the following way
case when ISNULL(ISNUMERIC(c.npinumber), 'blah') = 1
then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Building on Dhruvesh's answer,
case
when ISNUMERIC(c.npinumber) = 1 then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Will produce NULL anytime NpiNumber is NULL. The reason is that NULL + any string will still return NULL. The solution is to simply use the COALESCE function
case
when ISNUMERIC(c.npinumber) = 1 then c.NPiNUmber
else 'not valid: ' + COALESCE(c.NpiNumber, 'NULL VALUE')
end as npi
select ISNUMERIC(ISNULL(NULL, 'blah')),
ISNUMERIC(ISNULL(1234, 'blah')),
ISNUMERIC(ISNULL('ab', 'blah'))
Returns 0, 1, 0 - so your logic is correct.
When SQL's not behaving I like to simplify my query. Try running the query without your case statement first. If the results look right, then add additional logic.
What collation is your database? It's always a good idea to keep your column names properly cased (I'm looking at that all-lowercase column name over there...).
You don't require ISNULL. ISNUMERIC will return 1 if it's numberic or 0 if it's NULL or non-numeric.
case
when ISNUMERIC(c.NpiNumber) = 1 then c.NPiNUmber
else 'not valid: ' + c.NpiNumber
end as npi
Also as Euric Mentioned you may want to look at your all-lowercase column name.
Can anyone tell me how to translate the following T-SQL statement:
SELECT fileld1 = CASE
WHEN T.option1 THEN -1
ELSE
CASE WHEN T.option2 THEN 0
ELSE 1
END
END
FROM Table1 AS T
The point is I need to validate two different options from the table for a single field in the select statement..
I have tried to do somthing with an IF statement in pl/sql, but it just doesnt work for me:
SELECT IF T.option1 THEN -1
ELSE IF T.option2 THEN 0
ELSE 1
END
FROM Table1 AS T
I am not actually sure how to write IF statement inside the SELECT statement..
And also, I need to do it INSIDE the select statement because I am constructing a view.
Use:
SELECT CASE
WHEN T.option1 = ? THEN -1
WHEN T.option2 = ? THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
I can't get your original TSQL to work - I get:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
...because there's no value evaluation. If you're checking if the columns are null, you'll need to use:
SELECT CASE
WHEN T.option1 IS NULL THEN -1
WHEN T.option2 IS NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
...or if you need when they are not null:
SELECT CASE
WHEN T.option1 IS NOT NULL THEN -1
WHEN T.option2 IS NOT NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
CASE expressions shortcircuit - if the first WHEN matches, it returns the value & exits handling for that row - so the options afterwards aren't considered.
If I remember correctly, PL/SQL also supports the case. You just would have to move the column alias from "field1=" before the expression to "AS filed1" after the expression.
I have a huge query which uses case/when often. Now I have this SQL here, which does not work.
(select case when xyz.something = 1
then
'SOMETEXT'
else
(select case when xyz.somethingelse = 1)
then
'SOMEOTHERTEXT'
end)
(select case when xyz.somethingelseagain = 2)
then
'SOMEOTHERTEXTGOESHERE'
end)
end) [ColumnName],
Whats causing trouble is xyz.somethingelseagain = 2, it says it could not bind that expression. xyz is some alias for a table which is joined further down in the query. Whats wrong here? Removing one of the 2 case/whens corrects that, but I need both of them, probably even more cases.
SELECT
CASE
WHEN xyz.something = 1 THEN 'SOMETEXT'
WHEN xyz.somethingelse = 1 THEN 'SOMEOTHERTEXT'
WHEN xyz.somethingelseagain = 2 THEN 'SOMEOTHERTEXTGOESHERE'
ELSE 'SOMETHING UNKNOWN'
END AS ColumnName;
As soon as a WHEN statement is true the break is implicit.
You will have to concider which WHEN Expression is the most likely to happen. If you put that WHEN at the end of a long list of WHEN statements, your sql is likely to be slower. So put it up front as the first.
More information here: break in case statement in T-SQL
declare #n int = 7,
#m int = 3;
select
case
when #n = 1 then
'SOMETEXT'
else
case
when #m = 1 then
'SOMEOTHERTEXT'
when #m = 2 then
'SOMEOTHERTEXTGOESHERE'
end
end as col1
-- n=1 => returns SOMETEXT regardless of #m
-- n=2 and m=1 => returns SOMEOTHERTEXT
-- n=2 and m=2 => returns SOMEOTHERTEXTGOESHERE
-- n=2 and m>2 => returns null (no else defined for inner case)
If logical test is against a single column then you could use something like
USE AdventureWorks2012;
GO
SELECT ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO
More information - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-2017