How to write IF(expr1,expr2,expr3) in mssql - sql-server

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.

Related

oracle to sql sever query translation

Can someone help me to translate the below Oracle query to SQL SERVER syntax ?
with cte as(
select to_date('05-05-2022','mm-dd-yyyy') as eff_dt,
to_date('12-31-2022', 'mm-dd-yyyy') as exp_dt from dual
)
select eff,exp,round(months_between(exp,eff)) from(
select case when level = 1 then eff_dt
else add_months(trunc(eff_dt,'Y'),12*(level-1)) end as eff,
last_day(add_months(trunc(eff_dt,'Y'),12*level-1)) as exp
from
cte connect by level<=extract(year from exp_dt)-extract(year from eff_dt)+1)
;
Learn standard SQL language. MS SQL Server use standard SQL while Oracle has always wanted to go on its own dialect, despite the fact that Jim Melton, the technical mentor of Oracle was the reporter of the standard ISO SQL !
Well in any good book, you will find that CAST is the normative function to CAST... and CTE (Common Table Expression) is the normative construction to write recursive queries.
On this topic, you can use the paper I wrote many years ago : "Recursive Queries in SQL:1999 and SQL Server 2005"

Can this SQL CASE WHEN statement be made shorter?

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.

How can some letter hide in SQL Server?

I want to some column masking in sql column.
How can I do sql command?
Assuming you are using SQL Server 2008 or later, we can try using REPLICATE with SUBSTRING:
SELECT
'**' + SUBSTRING(col, 3, 2) + REPLICATE('*', LEN(col) - 4) AS mask
FROM yourTable;
Demo
A nicer way to handle this would be to make use of regular expressions, but SQL Server does not really support a regex replace. So I offer this as an alternative.

SQL Server Output Clause equivalent in Sybase

Is there a SQL Server Output clause equivalent in Sybase Version 15? As far as I know there's no Output clause in Sybase which gives the output rows from Update, delete query.
Indeed there is no directly identical statement-level clause. However you can achieve the same effect with a trigger.

How to convert or rewrite Access IIF Statement in SQL Server

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.

Resources