How to write nested-if in SQL server - sql-server-2012

I'm pretty new to SQL server. How to write nested-if statements with only one else clause in SQL server 2012. I want the sql code to check for the below example
select col1 = if(a>5 and b>3)
{ if(c>5 and d>3)
{if(e>7 and f>8) then "High"
}}
else "Low" from tbl;

It seems that more than "nested", this just are a lot of ANDs. This can be done with a CASE expression:
SELECT CASE
WHEN a > 5 AND b > 3
AND c > 5 AND d > 3
AND e > 7 AND f > 8 THEN 'High'
ELSE 'Low'
END col1
FROM ...
;

Related

Using Nested Case Expression

I am stuck in properly using Nested Case Expression.
The requirement is to create the calculated column that will be either 0 or 1.
If the name is 'abc' and ind = 1, then the value of calc column will be 1 . But if the name is 'abc' and salary is > 2000 . then the value will be 1 again irrespective the value of ind.
I have written the below query using OR condition in CASE Expression but want to use the Nested CASE.
select t.*, case when (name = 'abc' and ind = 1 ) or (name = 'abc' and sal > 2000)
then 1
else 0
end
from test t
Below is the result:
You don't need nested CASE:
-- multiple conditions
select t.*, case when name = 'abc' and ind = 1 then 1
when name = 'abc' and sal > 2000 then 1
else 0 end
from test t
I don't know why you need nested case expression, i would do instead of nested case expression that would be more efficient :
select t.*, (case when name = 'abc' and (ind = 1 or sal > 2000)
then 1
else 0
end)
from test t;
If you HAVE to have nested case expression then this is how you would write it:
SELECT t.*,
CASE
WHEN name = 'abc' THEN
CASE
WHEN ind = 1 THEN 1
WHEN sal > 2000 THEN 1
ELSE 0
END
ELSE 0
END
FROM test t;
If you are on SQL Server 2012 and above, you can also use an IIF statement like below:
select iif(name = 'abc' AND (salary > 2000 OR ind = 1), 1,0)

Rewriting case when in sql

Hi Im trying to rewrite the following code that uses CASE WHEN. I was thinking that I can instead use decode or something else?
The code:
create table want as select
case when (Var1<20 ) then 1
when (40>Var1>=20 ) then 2
when (Var1>=40 ) then 3
else .
end as Var1
This is more simply written as:
create table want as
select (case when Var1 < 20 then 1
when Var1 < 40 then 2
when Var1 >= 40 then 3
else NULL
end) as Var1
decode() only supports equality. One of the reasons why we welcomed case() when it was introduced back in the day was precisely because it allowed us to test greater than and less than.
However it is possible to nest decode() calls and combine them with other functions to achieve the same outcome. This ...
select id
, var1
, decode(greatest(var1,20), 20, 1,
decode(least(var1,40), 40, 3, 2 )) as trans_var
from tab
/
... implements the logic of your case() statement:
select id
, var1
, (case when Var1 < 20 then 1
when Var1 < 40 then 2
when Var1 >= 40 then 3
else NULL
end) as trans_var
from tab
/
SQL Fiddle demo.

select query to identify numeric or alphanumeric values

I have a table like below:
TN Tier
90 1
90 N3
30 2
40 3
50 A
"Tier" column may contain numeric as well as alpha-numeric values for any TN. I want to run a select query on above table so that if for any TN, there are both(numeric and alpha-numeric) values present in Tier column then it should be called as "Mix" otherwise "Numeric" or "Non-Numeric".
Desired Output :
TN Result
90 Mix
30 Numeric
40 Numeric
50 Non-Numeric
I am able to achieve it by using multiple temp tables but i want to avoid using temp tables. Any help would be appreciated!!!
You can take use advantage of ISNUMERIC() function in SQL Server.
SELECT [TN],
CASE MAX(ISNUMERIC(Tier)) + MIN(ISNUMERIC(Tier))
WHEN 2 THEN 'Numeric'
WHEN 1 THEN 'Mix'
ELSE 'Non-Numeric'
End As Result
FROM TableName
GROUP BY TN
Here's a Demo.
For MySQL, use REGEXP with a CASE expression:
SELECT
TN,
CASE WHEN SUM(CASE WHEN Tier REGEXP '[A-Z]' AND Tier REGEXP '[0-9]'
THEN 1 ELSE 0 END) > 0 THEN 'Mix'
WHEN SUM(CASE WHEN Tier REGEXP '[A-Z]'
THEN 1 ELSE 0 END) > 0 THEN 'Non-Numeric'
WHEN SUM(CASE WHEN Tier REGEXP '[0-9]'
THEN 1 ELSE 0 END) > 0 THEN 'Numeric'
ELSE 'Other' END AS Result
FROM yourTable
GROUP BY
TN;
For SQL Server, you may slightly alter the above query by using LIKE with an appropriate pattern:
SELECT
TN,
CASE WHEN SUM(CASE WHEN Tier LIKE '%[A-Z]%' AND Tier LIKE '%[0-9]%'
THEN 1 ELSE 0 END) > 0 THEN 'Mix'
WHEN SUM(CASE WHEN Tier LIKE '%[A-Z]%'
THEN 1 ELSE 0 END) > 0 THEN 'Non-Numeric'
WHEN SUM(CASE WHEN Tier LIKE '%[0-9]%'
THEN 1 ELSE 0 END) > 0 THEN 'Numeric'
ELSE 'Other' END AS Result
FROM yourTable
GROUP BY
TN;
The SQL Server answer given by #JohnWoo is tighter than this, but as you tagged with MySQL I initially answered for this database.
You dont need any big query it can be make with simple,
try this:
Select * from table
then you can check value of you column in while loop, each time you can search string having number or not number in you record.
$string = $row['Tier'];
if (preg_match('/[A-Za-z]/', $myString) )
{
echo 'String contains letters';
}
else if(preg_match('/[0-9]/', $myString))
{
echo 'String contains numbers';
}else if($string == '')
{
echo 'String contains no letter and no number';
}
I hope, you are using mysql. It is better if you can write a custom function as follows.
delimiter //
CREATE FUNCTION is_numeric(inputValue VARCHAR(50))
RETURNS INT
BEGIN
IF (inputValue REGEXP ('^[0-9]+$'))
THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
Example - select is_numeric(id),is_numeric(full_name) FROM tbl_user;

AND and OR operator in SQL Server

Is the AND operator in SQL Server equivalent to && or & in C# (or other languages)?
Ie, will it check for second condition if first condition is found to be false?
No, there is no short-circuiting in SQL. For example:
SELECT a, b, c
From T t
WHERE is_numeric(c) = 1 AND cast(c as numeric(10,2)) > 100.00
This may generate an invalid cast error where c is not numeric.
However you can force it to short-circuit by re-writing to use a CASE statement:
SELECT a, b, c
From T t
where
case
when is_numeric(c) = 0 then 0
when cast(c as numeric(10,2)) > 100.00 then 1
else 0
end = 1
In general and with the exception of handling nulls any clause of the form
where <EXPR1> AND <EXPR2> AND <EXP3>
can be written as:
where case
when NOT <EXPR1> then 0
when NOT <EXPR2> then 0
when NOT <EXPR3> then 0
else 1 end = 1
Any expression of the form:
where <EXPR1> OR <EXPR2> OR <EXPR3>
Can be written as:
where case
when <EXPR1> then 1
when <EXPR2> then 1
when <EXPR3> then 1
else 0 end = 1

SQL: Order by statement with start value

I have a field like this:
1月~3月
12月~1月
3月~12月
4月~12月
9月~8月
6月~7月
How can i sort that column following:
4月~12月
6月~7月
9月~8月
12月~1月
1月~3月
3月~12月
It start by 4 and end by 3 (4-5-6-7-8-9-10-11-12-1-2-3)(month)
This will do it, you need to separate out the numeric portion of your field, and also use a CASE statement:
SELECT *
FROM Table1
ORDER BY CASE WHEN CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT) >= 4 THEN 1 END DESC
,CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT)
Demo: SQL Fiddle
SQL Server syntax above, might vary depending on database.
order by case when col < 4 then 1 else 0 end, col
or if it's really a varchar
order by case when convert(int,substring(col,1,1)) < 4 then 1 else 0 end, col