SQL Server - Case Statement - sql

I'm almost certain you cannot do this within the context of the case statement, and I haven't been able to find any documentation about it, but is it possible to do the following:
SELECT CASE WHEN testValue > 2
THEN testValue(Without Repeating it) ELSE FailValue)
END
FROM Table
A better more thorough example:
Select CASE WHEN (Foo-stuff+bar) > 2
THEN Conditional statement without >2 Else "Fail"
END
FROM TABLE
I am looking for a way to create a select without repeating the conditional query.
EDIT: Due to a poor example on my part, and the lack of answers I was looking for:
testValue = (Table.A / Table.B) * Table.C Table.D
SELECT CASE WHEN testValue > 2
THEN testValue ELSE FailValue)
END
FROM Table

Like so
DECLARE #t INT=1
SELECT CASE
WHEN #t>0 THEN
CASE
WHEN #t=1 THEN 'one'
ELSE 'not one'
END
ELSE 'less than one'
END
EDIT:
After looking more at the question, I think the best option is to create a function that calculates the value. That way, if you end up having multiple places where the calculation needs done, you only have one point to maintain the logic.

The query can be written slightly simpler, like this:
DECLARE #T INT = 2
SELECT CASE
WHEN #T < 1 THEN 'less than one'
WHEN #T = 1 THEN 'one'
ELSE 'greater than one'
END T

I am looking for a way to create a select without repeating the conditional query.
I'm assuming that you don't want to repeat Foo-stuff+bar. You could put your calculation into a derived table:
SELECT CASE WHEN a.TestValue > 2 THEN a.TestValue ELSE 'Fail' END
FROM (SELECT (Foo-stuff+bar) AS TestValue FROM MyTable) AS a
A common table expression would work just as well:
WITH a AS (SELECT (Foo-stuff+bar) AS TestValue FROM MyTable)
SELECT CASE WHEN a.TestValue > 2 THEN a.TestValue ELSE 'Fail' END
FROM a
Also, each part of your switch should return the same datatype, so you may have to cast one or more cases.

We can use case statement Like this
select Name,EmailId,gender=case
when gender='M' then 'F'
when gender='F' then 'M'
end
from [dbo].[Employees]
WE can also it as follow.
select Name,EmailId,case gender
when 'M' then 'F'
when 'F' then 'M'
end
from [dbo].[Employees]

Related

SQL Statement Case When With Select * Syntax

I have a SQL Statement as below:
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
just wondering why it will prompt me error "From key world no found." If i change my statement in such way:
SELECT DRV_Name,DRV_ID ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
it working perfectly. Just curious isn't i need to type out all the field name 1 by 1 instead of using '*'?
You need to alias your table.
SELECT
d.*,
CASE WHEN DRV_ID IS NOT NULL THEN
CASE WHEN (DRV_ID)<'100' THEN
CONCAT(rental_type_c,'*')
END
ELSE
'9'
END as testing99
FROM
dmtb_driver d;
I think your logic could use some simplification too.
Ii think you missed space before from and concat, also I don't think so two case statement is required.
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL AND (DRV_ID)<'100' then concat(rental_type_c,'*') ELSE '9' end) as testing99
FROM dmtb_driver;

Return Bit Value as 1/0 and NOT True/False in SQL Server

I have a Table in SQL Server 2000 with BitValue Column. But, it is being displayed as True/False in SQL Server Management Studio. When I do a Select * from Tablename it returns the BitValue Column values as True/False.
How do I force it to return the value as bits (1/0) instead of True/False?
Any Help will be really appreciated?
Try with this script, maybe will be useful:
SELECT CAST('TRUE' as bit) -- RETURN 1
SELECT CAST('FALSE' as bit) --RETURN 0
Anyway I always would use a value of 1 or 0 (not TRUE or FALSE). Following your example, the update script would be:
Update Table Set BitField=CAST('TRUE' as bit) Where ID=1
Modify your query to generate the output that you want.
Try casting them to int:
select cast(bitFlag as int)
Or, if you like, use case:
select (case when bitFlag = 0 then 0 else 1 end)
This can be changed to 0/1 through using CASE WHEN like this example:
SELECT
CASE WHEN SchemaName.TableName.BitFieldName = 'true' THEN 1 ELSE 0 END AS 'bit Value'
FROM SchemaName.TableName
Try this:- SELECT Case WHEN COLUMNNAME=0 THEN 'sex'
ELSE WHEN COLUMNNAME=1 THEN 'Female' END AS YOURGRIDCOLUMNNAME FROM YOURTABLENAME
in your query for only true or false column
just you pass this things in your select query. using CASE
SELECT
CASE
WHEN gender=0 THEN 'Female'
WHEN gender=1 THEN 'Male'
END
as Gendership from Tablename;

SQL case with different fields to check from same table

I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,

SQL Query - Return Text based on numeric value

I am trying to work out a query where the query will perform a count (total) on a specific column. If the count is greater than 0, I want to display YES and display NO if the returned count is zero.
So, if I a query as this:
SELECT COUNT(ProblemID)
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')
that will actually be a subquery, how do I get the subquery to display "YES" when the returned count is greater than 0 and NO if the count is 0?
I appreciate any insight and help.
select isnull(
SELECT MAX('YES')
FROM dbo.ProblemInfo
WHERE ProblemID IN (100,101,309,305,205,600,500)
AND DEPID = '10866'),
'NO')
This is a trick to return either YES if there's at least one matching row, or null if not.
The wrapping isnull call then turns a null into a NO
Here's an alternate way of querying that.
IF EXISTS(
SELECT *
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500))
AND (DEPID = '10866')
)
BEGIN
SELECT 'Yes'
END
ELSE
BEGIN
SELECT 'No'
END
What I like about this method is that, for enormous data-sets, it should be noticeably faster.
try
SELECT CASE
WHEN (SELECT COUNT(ProblemID) FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')) > 0
THEN 'YES'
ELSE 'NO' END
FROM YourTable
you can use case when.
SELECT
case
when COUNT(ProblemID) = 0 then 'NO'
else 'YES'
end
FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')

Can we have multiple case then statements in sql

I am trying for something which requires case within case I just wanted to make sure if we can use multiple case then?I am running this on sql teradata
The code I am trying to use is as below
AND(
case when CHARACTER_LENGTH(drug.n)=0 then 0
when CHARACTER_LENGTH(drug.n)=1 then
(case when substring(drug.n from 1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end)
when CHARACTER_LENGTH(drug.n)=2 then
(case when substring(drug.n from 1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
case when substring(drug.n from 2,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end )
when CHARACTER_LENGTH(drug.n)=3 then
(case when substring(drug.n from 1,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
case when substring(drug.n from 2,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end *
case when substring(drug.n from 3,1) in (''0'',''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9'') then 1 else 0 end )=1
If somebody has better idea you can let me know. I cannot use isnumeric function.
Yes you can use nested CASE statements. No problems with that in Teradata
Okay -
To determine whether an arbitrary length string contains only numeric characters (or does not), you can use a recurive CTE.
Please note that I don't know whether or not your RDBMS actually supports recursive CTEs, but this is a potential solution. Also, I'm not sure of the performance implications - however, it does remove the multiple CASE effect (And why isn't that an actual numeric field, anyways?).
So... For a table that looks like this:
id ch
================
1 1234567890
2 asdg
This statement returns all rows that contain only numeric characters (of any length):
WITH splitstring (id, chard, start, orig) as (
SELECT id, SUBSTRING(ch, 1, 1), 1, ch
FROM chartable
UNION ALL
SELECT id, SUBSTRING(orig, start + 1, 1), start + 1, orig
FROM splitstring
WHERE LENGTH(orig) > start)
SELECT *
FROM chartest as a
WHERE NOT EXISTS (SELECT '1'
FROM splitstring as b
WHERE a.id = b.id
AND chard NOT BETWEEN '0' AND '9')
Without some of the larger context it's somewhat difficult to know exactly what you're trying to accomplish. However, this should be adaptable for your needs.
(As a side note, DB2 for the iSeries doesn't seem to support regex either...)