January 10, 2023 at 12:26 pm
Hi,
I have this SQL fragment
SELECT COL1,
CASE WHEN Table1.COL2 = 0 then 'ZERO'
WHEN Table1.COL2 = 1 then 'ONE'
ELSE '' END AS MyResult
FROM Table1
Which is the best way to treat Table1.COL2 NULL values as if they were 1 ?
Is this the best way ?
WHEN ISNULL(Table.COL2,1) then 'ONE'
Thank you
January 10, 2023 at 1:18 pm
COALESCE(TABLE1.COL2,1) ? equals if COL2 is null, treat it as value "1"
January 10, 2023 at 1:21 pm
COALESCE vs ISNULL https://www.mssqltips.com/sqlservertip/2689/deciding-between-coalesce-and-isnull-in-sql-server/
I prefer COALESCE because it is known by other SQL-dialects and it doesn't truncate.
In where-clauses I would use: where X is NULL or X=1
instead of COALESCE (unless you use computed indexed)
January 10, 2023 at 1:50 pm
Hi, I have this SQL fragment
SELECT COL1,
CASE WHEN Table1.COL2 = 0 then 'ZERO'
WHEN Table1.COL2 = 1 then 'ONE'
ELSE '' END AS MyResult
FROM Table1Which is the best way to treat Table1.COL2 NULL values as if they were 1 ? Is this the best way ?
WHEN ISNULL(Table.COL2,1) then 'ONE'Thank you
We will need your help in order to be able to help you, so please help us!
😎
It would be greatly appreciated if you could provide the DDL (create table) script, sample data as an insert statement, the desired output from the sample data, and what you have tried so far.
January 10, 2023 at 6:48 pm
ISNULL/COALESCE is not as clear as explicitly checking for NULL:
SELECT COL1,
CASE WHEN Table1.COL2 = 0 then 'ZERO'
WHEN Table1.COL2 = 1 OR Table1.Col2 IS NULL then 'ONE'
ELSE '' END AS MyResult
FROM Table1
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 11, 2023 at 2:28 am
ISNULL/COALESCE is not as clear as explicitly checking for NULL:
SELECT COL1,
CASE WHEN Table1.COL2 = 0 then 'ZERO'
WHEN Table1.COL2 = 1 OR Table1.Col2 IS NULL then 'ONE'
ELSE '' END AS MyResult
FROM Table1
That and the fact that neither column might be NULL.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply