TSQL IF THEN WHERE something ELSE WHERE something else - sql-server

I need to transform this c# line into a SQL query:
from t in table where type == 0 ? t.colA=1 : t.ColB=1
I've tried something like
select * from table t where
BEGIN IF #type=0 THEN t.colA=1 END
BEGIN IF #type=1 THEN t.colB=1 END
but I get
Incorrect syntax near the keyword 'BEGIN'.
and
Incorrect syntax near the keyword 'THEN'.
What am I doing wrong? It is even possible to do this as a SQL command?

Just use boolean expressions:
select *
from table t
where (#type = 0 and t.colA = 1) or
(#type = 1 and t.colB = 1)
If you want everything when #type is not 0 or 1, then include:
select *
from table t
where (#type = 0 and t.colA = 1) or
(#type = 1 and t.colB = 1) or
(#type not in (0, 1)) -- adjust if #type could be NULL

You could use CASE:
select * from table t
where (CASE #type WHEN 0 THEN colA
WHEN 1 THEN colB
END) = 1

CASE is the "if" function in TSQL
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-2017

Related

SQL Server - WHERE clause with CASE

ALTER PROCEDURE GetVendor_RMA_CreditMemo
(#HasCreditMemoNo INT)
BEGIN
SELECT
*
FROM
(SELECT
CreditMemoNumber,
CASE WHEN CreditMemoNumber != ''
THEN 1
ELSE 0
END AS HasCreditMemoNo
FROM
XYZ) as C
WHERE
(C.HasCreditMemoNo = #HasCreditMemoNo OR #HasCreditMemoNo = -1)
END
CreditMemoNumber is a varchar column
I want to achieve this:
CASE
WHEN #HasCreditMemoNo = 0
THEN -- select all rows with no value in CreditMemoNumber Column,
WHEN #HasCreditMemoNo = 1
THEN -- all rows that has some data,
WHEN #HasCreditMemoNo = -1
THEN -- everything regardless..
You can't do this kind of thing with a CASE.
The correct way to do it is with OR:
WHERE (#HasCreditMemoNo = 0 AND {no value in CreditMemoNumber Column})
OR
(#HasCreditMemoNo = 1 AND {all rows that has some data})
OR
(#HasCreditMemoNo = -1)
Would this work for you? I'm not sure if it would improve your performance. You may be better off writing an if else if else statement and three separate select statements with an index on the CreditMemoNumber column.
ALTER PROCEDURE GetVendor_RMA_CreditMemo(#HasCreditMemoNo int)
BEGIN
select
CreditMemoNumber,
case when CreditMemoNumber != '' then 1 else 0 end as HasCreditMemoNo
from XYZ
where
(#HasCreditMemoNo = 0 and (CreditMemoNumber is null or CreditMemoNumber = ''))
or (#HasCreditMemoNo = 1 and CreditMemoNumber != '')
or (#HasCreditMemoNo = -1)
END

How to convert IF function from Excel formula to TSQL?

= if( _DataCell_ILF_Cred_ = "", "",
_DataCell_ILF_Lyr_Pick * _DataCell_ILF_Cred + _DataCell_Burn_Lyr_Pick_ * (1 - _DataCell_ILF_Cred_)
)
I want to convert this Excel IF Function into SQL code.
You can use CASE expression, or IIF() if you are working on SQL Server 2012+:
SELECT CASE WHEN _DataCell_ILF_Cred_ = ',' THEN
DoSomething
END
If _DataCell_ILF_Cred_ <> ',', it will return NULL, if you want to return another value instead of NULL, you need to add ELSE like:
SELECT CASE WHEN _DataCell_ILF_Cred_ = ',' THEN
DoSomething
ELSE
DoSomethingElse
END
However, your the last part of your if is not clear, you need to provide more information (as datatypes) to get better answer.
Literal translation (with some mock data) would be as follows:
select
1 as '_DataCell_ILF_Cred_',
2 as '_DataCell_ILF_Lyr_Pick',
3 as '_DataCell_Burn_Lyr_Pick'
into #test
select * from #test
declare #_DataCell_ILF_Cred_ decimal
if ((select [_DataCell_ILF_Cred_] from #test) <> '')
begin
select #_DataCell_ILF_Cred_ = [_DataCell_ILF_Lyr_Pick] * [_DataCell_ILF_Cred_]
+ [_DataCell_Burn_Lyr_Pick] * (1 -[_DataCell_ILF_Cred_])
from #test
select #_DataCell_ILF_Cred_ as [_DataCell_ILF_Cred_]
end
Do not hesitate to check IF...ELSE documentation:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql

Error: No result rowset is associated with the execution of this query

Query which should be stored as variable in "Execute SQL Task":
Use [Reporting]
Go
DECLARE
#current_month DATETIME,
#current_year DATETIME,
#current_year_final AS INT,
#previous_period_final VARCHAR(2),
#test_period AS VARCHAR (2),
#previous_period_final_2 VARCHAR(2)
SET #current_month = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0)
SET #current_year = DATEADD(YEAR,DATEDIFF(YEAR,0,GETDATE()),0)
SET #current_year_final = CASE WHEN LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),4),2) = '01' THEN LEFT(CONVERT(VARCHAR,#current_year,112),4)-1 ELSE LEFT(CONVERT(VARCHAR,#current_year,112),4) END
SET #previous_period_final = CASE
WHEN CAST(LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),4),2) AS INT) - 1 BETWEEN 10 AND 11 THEN CAST(CAST(LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),4),2) AS INT) - 1 AS VARCHAR) -- Type Varchar
WHEN CAST(LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),3),1) AS INT) - 1 BETWEEN 1 AND 8 THEN '0' + CAST(CAST(LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),3),1) AS INT) - 1 AS VARCHAR)
WHEN CAST(LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),4),2) AS INT) - 1 = 9 THEN '09'
WHEN LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),4),2) = '01' THEN '12'
ELSE 'Invalid formula' END
SET #previous_period_final_2 = CAST(LEFT(RIGHT(CONVERT(VARCHAR, GETDATE(),112),4),2) AS INT) - 1
-- ### Returns:
-- 1 = 'identical' = row numbers match
-- 0 = 'mis-matched' = row numbers do not match
select sum(rows_identical) as rows from
(
select CAST(CAST(case when count_rows_raw = count_rows_facts then 1 else 0 end as bit)as int) as rows_identical
--,SQL_VARIANT_PROPERTY(CAST(CAST(case when count_rows_raw = count_rows_facts then 1 else 0 end as bit)as int),'BaseType') -- ### EXCELLENT WAY TO RETRIEVE DATATYPE ###
from
(
select
(
select SUM(#rows_facts) as #rows_facts from
(
select COUNT(ID) as #rows_facts, 'finalized_entries_facts_table' as type from facts_reporting
where YearMonthID in (select YearMonthID from dim_year_month as yyMM
where yyMM.YearMonth = CAST(#current_year_final AS Varchar)+'_'+CAST(#previous_period_final AS Varchar))
and SourceID IN (select SourceID from dbo.dim_source where Source = 'Unlocks')
union
select COUNT(ID) as #rows_facts, 'missing_entries_facts_table' as type from missing_ids_Unlocks_raw
where YearMonthID in (select YearMonthID from dim_year_month as yyMM
where yyMM.YearMonth = CAST(#current_year_final AS Varchar)+'_'+CAST(#previous_period_final AS Varchar))
and SourceID IN (select SourceID from dbo.dim_source where Source = 'Unlocks')
) as rows
) as count_rows_facts,
(
SELECT count(*)as #rows_raw from UnlocksRawCSV_table as raw
Where Year_Month_inserted = CAST(#current_year_final AS Varchar) + '_' + #previous_period_final
) as count_rows_raw
)
as row_checks
)
as count_check
--where rows_identical <> 0
Variable definition and Result Set mapping in SSIS:
In the Execute SQL Task I have selected "Result Set = Single row". I have also tried "None". I have put a post-execute Breakpoint on the Execute SQL Task and get this:
== Why am I getting a 0 as value in SSIS while I am getting a 1 if I execute this query (see above) in SQL Server directly?
The precedence constraint in the subsequent task look like this:
EDIT 1: Changing this expression to #RowCheck == 0 does not change anything as the task still fails on execution with the same error:
Error: No result rowset is associated with the execution of this query
EDIT: 2: Changing the Value in the Precedence Constraint Editor from "Success" to "Failure" works but I don't understand why the task itself fails and why it generates the above mentioned error message? However, despite proceeding with the package the value displayed for variable RowCheck does not make any sense as it is always zero, regardless of that the Query returns...
EDIT 3: Screen of General Tab for Execute SQL Statement (I have also tried with "Single Row" and above mentioned Result Set Mapping:

CASE statement inside a WHERE clause in SQL Server

I am trying to add a CASE statement, but I need an OR within it and I am having a little trouble.
I'm not sure how I can do this, but I was hoping someone could help. Here is what I am trying to do:
SELECT *
FROM Table1
WHERE IsUpdate = CASE WHEN #Type = 'Yes' THEN 1 ELSE (0 OR 1) END
So basically, I want to select only the rows that have IsUpdate set to 1 when #Type = 'Yes', otherwise I want to select the rows where IsUpdate = 0 OR IsUpdate = 1. Any ideas?
You don't need a CASE, i assume that the value can only be 0 or 1:
SELECT * FROM Table1
WHERE #Type <> 'Yes' OR IsUpdate = 1
If this is sitting in a stored-procedure it's probably better to use a If-Else instead of the parameter-check since above query is non-sargable so it might be inefficient on a large table.
The full where clause that matches your logic is:
where (#Type = 'Yes' and IsUpdate = 1) or
(#Type <> 'Yes' and IsUpdate in (0, 1))
You can simplify this, if you know something about the values in the columns. For instance, if IsUpdate only takes on the values 0 and 1 (and not NULL):
where #Type <> 'Yes' or IsUpdate = 1

Check if a SQL Select statement returns no rows

I have a select statement
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = #MerchantId)
I want to check if that statement returns 0 rows. I tried to use the ISNULL and IFNULL but it seems that I'm missing something.
To find out whether no matching rows exist you can use NOT EXISTS. Which can be more efficient than counting all matching rows
IF NOT EXISTS(SELECT * FROM ...)
BEGIN
PRINT 'No matching row exists'
END
If this is SQL Server, try ##ROWCOUNT.
SELECT COUNT(*)
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = #MerchantId)
If you get 0, you got 0. :)
You can use ##ROWCOUNT. For e.g.
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = #MerchantId)
--This will return no of rows returned by above statement.
SELECT ##ROWCOUNT
You will get 0 if first statement will not return any rows. You can also use if statement to check that just after first statement. e.g.
IF ##ROWCOUNT <> 0
PRINT 'Select statement is returning some rows'
ELSE
PRINT 'No rows returned'
try this:
SELECT ISNULL(QBalance, 'ReplaceValue')
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = #MerchantId)
Could also use an outer ISNULL check?
SELECT ISNULL((
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = #MerchantId)), 0)

Resources