Are there any features within Microsoft SQL Server TSQL that could shorten this CASE WHEN statement?
CASE
WHEN some_column IS NULL
THEN 0
ELSE 1
END
For SQL Server 2012 and later you can use IIF() statement.
SELECT IIF(some_column IS NULL , 0 , 1)
You could use what SQL Server documentation calls the "simple" case expression, instead of the "search" case expression that the syntax in the question uses.
case some_column when null then 0 else 1 end
Not a large difference, but it is shorter.
Related
trying to convert DB2 query ORDER BY condition into SQL Server
DB2 Query
ORDER BY
CASE WHEN LEN(RTRIM(LTRIM(CorpName))) > 1 THEN CorpVal Else '999' END,
CASE WHEN SUBSTRING(FName,1,1) != '*' THEN FName Else '999' END
SQL Query
ORDER BY
CASE WHEN CorpName like '[a-z]%' THEN 0 ELSE 1 END,
CASE WHEN FName like '[a-z]%' THEN 0 ELSE 1 END
I have the data something like
ABC,
24KS,
ABE,
AJX,
-Extra,
ABF,
1X1
I need the output like below
ABC,
ABE,
ABF,
AJX,
24KS,
1X1,
-Extra
this does not works for me, need some more suggestion.
Ordering is determined by collations in SQL Server and DB2. It seems your iSeries DB2 is configured with an EBCDIC collation so you could add an explict COLLATE clause to the ORDER BY expression to coerce EBCDIC ordering rules for SQL Server since your SQL Server collation is apparently different.
Below is an example of your original DB2 query with the clause added for the SQL Server:
ORDER BY
CASE WHEN LEN(RTRIM(LTRIM(CorpName))) > 1 THEN CorpVal Else '999' END COLLATE SQL_EBCDIC037_CP1_CS_AS,
CASE WHEN SUBSTRING(FName,1,1) != '*' THEN FName Else '999' END COLLATE SQL_EBCDIC037_CP1_CS_AS
An old employee left a massive query behind that I've been debugging and it appears that the issue has come down to SQL Server itself determining a comparison differently than what I would have expected.
I have a table with a column col1 containing the value 20191215 as a datetime.
The part in question is similar to the following:
select case when col1 > '01/01/2020' then 1 else 0 end
This statement is returning 1, suggesting that '12/15/2019' is larger than '01/01/2020'.
I do not need assistance correcting the query, as I have already made changes to do so other than using the comparison the previous employee was using, I am simply curious as to why SQL Server would evaluate this as I have described.
I understand that this is not the typically way SQL Server would store dates as well, would the issue simply be the formatting of the dates?
Current SQL Server version is: SQL Server 2014 SP3 CU3.
SQL Fiddle link that shows the same results
Please note that the link does not contain an exact replica of my case
Edit: Included additional info relevant to actual query.
It is a string comparison not a date comparison:
select case when '12/15/2019' > '01/01/2020' then 1 else 0 end
vs
select case when CAST('12/15/2019' AS DATE) > CAST('01/01/2020' AS DATE) then 1 else 0 end
db<>fiddle demo
I am simply curious as to why SQL Server would evaluate this as I have described.
'12/15/2019' it is a string literal, SQL Server does not know you want to treat a date unless you explicitly express your intention.
I have a table with a column col1 containing the value 20191216
If you are comparing with a column then the data type of column matters and data type precedence rules
I have the following query that I wanted to convert to in SQL Server but not sure how I should go about this. Should I use a CASE statement because I don't see any true/false conditions in this statement. How should I go about this? Thanks!
ORACLE Query:
IIF(CD_ID = '999999',TO_CHAR(CL_ID) || TO_CHAR(CD_SEQ_NO) || '999.99',TO_CHAR(CL_ID) || TO_CHAR(CD_SEQ_NO) || TO_CHAR(CD_ID))
From SQL SERVER 2012+ we can use CONCAT function.
SELECT IIF(CD_ID = '999999',CONCAT(CD_SEQ_NO, '999.99'),CONCAT(CL_ID,CD_SEQ_NO,CD_ID))
FROM YOURTABLE
Concat function not only concatenate the strings but also does implicit conversion when required
Anything less then SQL SERVER 2012 use CASE statements with explicit conversions to varchar
SELECT CASE
WHEN cd_id = '999999' THEN Cast(cd_seq_no AS VARCHAR(50)) + '999.99'
ELSE Cast(cl_id AS VARCHAR(50))+Cast(cd_seq_no AS VARCHAR(50))+Cast(cd_id AS VARCHAR(50))
END
FROM yourtable
I thought the IIf statements returns one value if the condtion is true or false BUT This IIf statement in Access returns the field and it values.
IIf([A1]![KPr],[A1]![Kat],IIf([Data2]![Kat],[Data2]![Kat],[Data1]![Kat])),
the table left join in the from clause
I'm try to realize this statement in SQL Server using CASE WHEN but it also accepts a true or false condition.
How can I understand and realize this statement.
IIf function in VB, VBA, and Access is the same as ps_prakash02 wrote in the comment: iif(condition, value_if_true, value_if_false). this means that if the condition evaluates to true, the value_if_true is returned, otherwise value_if_false returns.
So a translation of IIf to t-sql is simply CASE WHEN condition THEN value_if_true ELSE _value_if_false END.
I'm not so sure what [A1]![KPr] means in access, I'm guessing it's KPr column value of table A1 or something like this, so I'll leave them as they are in your question and just replace the IIF with CASE in my answer:
CASE WHEN [A1]![KPr] THEN [A1]![Kat]
ELSE
CASE WHEN [Data2]![Kat] THEN [Data2]![Kat]
ELSE [Data1]![Kat]
END
END
Note: In SQL Server 2012 Microsoft included IIF in t-sql.
There is IF(expr1,expr2,expr3) in my sql.
How to accomplish it in MS SQL?
You can use a CASE expression:
CASE WHEN expr1 THEN expr2 ELSE expr3 END
By the way, this syntax isn't SQL Server specific - it also works in MySQL and most other databases.