Multiple case statement not working as expected in Postgres - sql

This is the query:
SELECT DISTINCT
completed_phases,
CAST(completed_phases::bit(8) AS VARCHAR),
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=1 THEN 'FT' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=2 THEN 'ED' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=3 THEN 'MC' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=4 THEN 'HC' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=5 THEN 'UV' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=6 THEN 'TT' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=7 THEN 'RX' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=8 THEN 'PI' ELSE '' END
FROM rx_sales_order
If completed_phase is 129, my output for final column should be FTPI. But it is only showing FT. Only the first case statement seems to work, even if all of them are distinct.

STRPOS() will always return the first occurance of the searched string. So all calls to strpos() will return 1 for the input value 129.
You can use substring() instead:
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),1,1)='1' THEN 'FT' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),2,1)='1' THEN 'ED' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),3,1)='1' THEN 'MC' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),4,1)='1' THEN 'HC' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),5,1)='1' THEN 'UV' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),6,1)='1' THEN 'TT' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),7,1)='1' THEN 'RX' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),8,1)='1' THEN 'PI' ELSE '' END
Another option would be to use get_bit() to test each bit individually:
case when get_bit(completed_phases::bit(8), 0) = 1 then 'FT' else '' END||
case when get_bit(completed_phases::bit(8), 1) = 1 then 'ED' else '' END||
case when get_bit(completed_phases::bit(8), 2) = 1 then 'MC' else '' END||
case when get_bit(completed_phases::bit(8), 3) = 1 then 'HC' else '' END||
case when get_bit(completed_phases::bit(8), 4) = 1 then 'UV' else '' END||
case when get_bit(completed_phases::bit(8), 5) = 1 then 'TT' else '' END||
case when get_bit(completed_phases::bit(8), 6) = 1 then 'RX' else '' END||
case when get_bit(completed_phases::bit(8), 7) = 1 then 'PI' else '' END
A more flexible way of doing that is to turn the bits into rows and use an array as a lookup. Something like:
with lookup (codes) as (
values (array['FT','ED','MC','HC','UV','TT','RX','PI'])
)
SELECT completed_phases,
completed_phases::bit(8),
x.code
FROM rx_sales_order
join lateral (
select string_agg(codes[i],'') as code
from lookup, unnest(string_to_array(completed_phases::bit(8)::text, null)) with ordinality as t(b,i)
where b = '1'
) as x on true
The part regexp_split_to_table(completed_phases::bit(8)::text, '') with ordinality as t(b,i) will return the following for the value 129:
b | i
--+--
1 | 1
0 | 2
0 | 3
0 | 4
0 | 5
0 | 6
0 | 7
1 | 8
code[i] the uses the index to lookup the matching code and string_agg() then puts all selected codes together again into a single string. The condition where b = '1' only selects the bits that are set.
That solution will be substantially slower than the hardcoded case expression (because it increases the number of rows, just to reduce them again) - but it is more flexible and easier to maintain.
If you need that a lot, the best option would be to put the case expression into a function and use the function in your queries.
create or replace function get_codes(p_phases integer)
returns text
as
$$
select
case when get_bit(p_phases::bit(8), 0) = 1 then 'FT' else '' END||
case when get_bit(p_phases::bit(8), 1) = 1 then 'ED' else '' END||
case when get_bit(p_phases::bit(8), 2) = 1 then 'MC' else '' END||
case when get_bit(p_phases::bit(8), 3) = 1 then 'HC' else '' END||
case when get_bit(p_phases::bit(8), 4) = 1 then 'UV' else '' END||
case when get_bit(p_phases::bit(8), 5) = 1 then 'TT' else '' END||
case when get_bit(p_phases::bit(8), 6) = 1 then 'RX' else '' END||
case when get_bit(p_phases::bit(8), 7) = 1 then 'PI' else '' END
$$
language sql;
Then use:
SELECT DISTINCT
completed_phases,
get_codes(completed_phases) as codes
FROM rx_sales_order

As pointed out in the answer by a_horse_with_no_name, strpos will return the first occurrence of the searched string. At any rate, it's better to use get_bit instead of casting to VARCHAR to check if a bit is set. Also, instead of ||, you can use concat, which will handle nulls as blank strings. Your query could then be rewritten to:
SELECT DISTINCT
completed_phases,
CAST(completed_phases::bit(8) AS VARCHAR),
concat(CASE get_bit(completed_phases::bit(8), 0) WHEN 1 THEN 'FT' END,
CASE get_bit(completed_phases::bit(8), 1) WHEN 1 THEN 'ED' END,
CASE get_bit(completed_phases::bit(8), 2) WHEN 1 THEN 'MC' END,
CASE get_bit(completed_phases::bit(8), 3) WHEN 1 THEN 'HC' END,
CASE get_bit(completed_phases::bit(8), 4) WHEN 1 THEN 'UV' END,
CASE get_bit(completed_phases::bit(8), 5) WHEN 1 THEN 'TT' END,
CASE get_bit(completed_phases::bit(8), 6) WHEN 1 THEN 'RX' END,
CASE get_bit(completed_phases::bit(8), 7) WHEN 1 THEN 'PI' END)
FROM rx_sales_order;
On a side note, if you have the option to do so, I would recommend changing your database schema to store the phases as individual boolean columns instead of using a bit map. See Any disadvantages to bit flags in database columns? for a good discussion of why.

Related

Separate concatenated Case When statements

I want to know if there is a way to separate this statement and create a sentence like One, Two - or Two, Three. The separator must not work unless there is something following.
case when d1.code = 'X' then 'One' else '' end +
case when d2.Code = 'Y' then 'Two' else '' end +
case when d3.code = 'Z' then 'Three' else '' end as PlzHelp
If you're on a recent version of SQL Server, use CONCAT_WS:
SELECT CONCAT_WS(',', CASE WHEN d1.code = 'X' THEN 'One' END,
CASE WHEN d2.Code = 'Y' THEN 'Two' END,
CASE WHEN d3.code = 'Z' THEN 'Three' END) AS PlzHelp
...
If you are on an older version, you could go for something like this
SELECT RTRIM(SUBSTRING(CASE WHEN d1.code = 'X' THEN ', One' ELSE '' END
+CASE WHEN d2.Code = 'Y' THEN ', Two' ELSE '' END
+CASE WHEN d3.code = 'Z' THEN ', Three' ELSE '' END
+' ' ,3 ,17)) AS PlzHelp

Display Column value according to need in sql

if i have a column in which ten digit values occurs like Column A = 11111000 so how to show this value in sql like (List,Add,Edit,Delete,Export) in sql.
there is a condition means if first position have 1 then it show List, If second Position have 1 it show Add, third position have 1 then Edit, fourth position have 1 then Delete, fifth position have 1 then Export.
If the value is a string, you can do:
select stuff( ((case when substring(a, 1, 1) = 1 then ',List' else '' end) +
(case when substring(a, 2, 1) = 1 then ',Add' else '' end) +
. . .
), 1, 1, '')
The logic is similar for bitwise operators:
select stuff( ((case when a & 2^9 then ',List' else '' end) +
(case when 2 & 2^8 then ',Add' else '' end) +
. . .
), 1, 1, '')
Maybe Substring function can help you to identify these values
select
id, ColumnA,
case when substring(ColumnA,1,1) = '1' then 'X' end as List,
case when substring(ColumnA,2,1) = '1' then 'X' end as [Add],
case when substring(ColumnA,3,1) = '1' then 'X' end as Edit,
case when substring(ColumnA,4,1) = '1' then 'X' end as [Delete],
case when substring(ColumnA,5,1) = '1' then 'X' end as [Export]
from Table1
I update the query according to comment
select
id, ColumnA,
stuff(
case when substring(ColumnA,1,1) = '1' then ',List' else '' end +
case when substring(ColumnA,2,1) = '1' then ',Add' else '' end +
case when substring(ColumnA,3,1) = '1' then ',Edit' else '' end +
case when substring(ColumnA,4,1) = '1' then ',Delete' else '' end +
case when substring(ColumnA,5,1) = '1' then ',Export' else '' end
,1,1,''
)
from Table1

Combining SQL string alias into comma delimited string

I am working on this stored proc which check to see which BIT fields are true and depending on it assigns a text to it using alias name. I want to be able to combine these alias values (string) and have it comma delimited to I can display it on my SSRS report.
Below is part of my stored proc.
,CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN 'Reservation Already Sent'
END AS ReservationAttached
,CASE
WHEN sr.[HPOfficeAttached] = 1 THEN 'H&P Office'
END AS HPOfficeAttached
WHEN sr.[NotesAttached] = 1 THEN 'Notes'
END AS NotesAttached
,CASE
WHEN sr.[OpPermitAttached] = 1 THEN 'Op Permit'
END AS OpPermitAttached
,CASE
WHEN sr.[TestResultsAttached] = 1 THEN 'Test Results'
END AS TestResultsAttached
,CASE
WHEN sr.[ConsultationReportsAttached] = 1 THEN 'Consultation Reports'
END AS ConsultationReportsAttached
,CASE
WHEN sr.[OtherAttached] = 1 THEN 'Other'
END AS OtherAttached
so lets say in case where only NotesAttached and ReservationAlreadySentAttached were only true ones then I want the end result to come out as Notes, Reservation Already Sent.
How do i concatenate these aliases ?
Another option is to build your string and then just remove the leading comma with STUFF()
Example (abbreviated)
Declare #YourTable table (ReservationAlreadySentAttached bit,HPOfficeAttached bit,NotesAttached bit)
Insert Into #YourTable values
(1,0,1)
Select NewValue = stuff(
case when ReservationAlreadySentAttached = 1 then ', Reservation Already Sent' else '' end
+case when HPOfficeAttached = 1 then ', H&P Office' else '' end
+case when NotesAttached = 1 then ', Notes' else '' end
,1,2,'')
From #YourTable
Returns
NewValue
Reservation Already Sent, Notes
EDIT - Just the Expression
NewValue = stuff(
case when sr.[ReservationAlreadySentAttached] = 1 then ', Reservation Already Sent' else '' end
+case when sr.[HPOfficeAttached] = 1 then ', H&P Office' else '' end
+case when sr.[NotesAttached] = 1 then ', Notes' else '' end
,1,2,'')
Not the prettiest solution...
RIGHT((CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END)
,LEN(CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END) - 1 )
Edit: You may have to take into consideration when NONE of them are 1 so you don't get Invalid length parameter passed to the right function.
CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN ', Reservation Already Sent' ELSE ''
END
+ CASE
WHEN sr.[HPOfficeAttached] = 1 THEN ', H&P Office' ELSE ''
END
+ CASE etc...
And wrap this in some function to remove the first comma + space.

How to count non-null/non-blank values in SQL

I have data like the following:
And what I want is to count the PONo, PartNo, and TrinityID fields with a value in them, and output data like this:
How can I do this counting in SQL?
select
Job_number, Item_code,
case when RTRIM(PONo) = '' or PONo is null then 0 else 1 end +
case when RTRIM(PartNo) = '' or PartNo is null then 0 else 1 end +
case when RTRIM(TrinityID) = '' or TrinityID is null then 0 else 1 end
as [Count]
from YourTable
Try this:
select Job_Number = t.Job_Number ,
Item_Code = t.Item_Code ,
"Count" = sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
)
from dbo.my_table t
group by t.Job_Number , t.Item_Code
If you want to exclude data where all the tested fields are null or empty, add a having clause:
having sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
) > 0
Easy!

"CASE" statement within "WHERE" clause in SQL Server 2008

I am working with a query which contains "CASE" statement within "WHERE" clause. But SQL Server 2008 is giving some errors while executing it. Can anyone please help me with the correct query? Here is the query:
SELECT
tl.storenum 'Store #',
co.ccnum 'FuelFirst Card #',
co.dtentered 'Date Entered',
CASE st.reasonid
WHEN 1 THEN 'Active'
WHEN 2 THEN 'Not Active'
WHEN 0 THEN st.ccstatustypename
ELSE 'Unknown'
END 'Status',
CASE st.ccstatustypename
WHEN 'Active' THEN ' '
WHEN 'Not Active' THEN ' '
ELSE st.ccstatustypename
END 'Reason',
UPPER(REPLACE(REPLACE(co.personentered,'RT\\\\',''),'RACETRAC\\\\','')) 'Person Entered',
co.comments 'Comments or Notes'
FROM
comments co
INNER JOIN cards cc ON co.ccnum=cc.ccnum
INNER JOIN customerinfo ci ON cc.customerinfoid=ci.customerinfoid
INNER JOIN ccstatustype st ON st.ccstatustypeid=cc.ccstatustypeid
INNER JOIN customerstatus cs ON cs.customerstatuscd=ci.customerstatuscd
INNER JOIN transactionlog tl ON tl.transactionlogid=co.transactionlogid
LEFT JOIN stores s ON s.StoreNum = tl.StoreNum
WHERE
CASE LEN('TestPerson')
WHEN 0 THEN co.personentered = co.personentered
ELSE co.personentered LIKE '%TestPerson'
END
AND cc.ccnum = CASE LEN('TestFFNum')
WHEN 0 THEN cc.ccnum
ELSE 'TestFFNum'
END
AND CASE LEN('2011-01-09 11:56:29.327')
WHEN 0 THEN co.DTEntered = co.DTEntered
ELSE
CASE LEN('2012-01-09 11:56:29.327')
WHEN 0 THEN co.DTEntered >= '2011-01-09 11:56:29.327'
ELSE co.DTEntered BETWEEN '2011-01-09 11:56:29.327' AND '2012-01-09 11:56:29.327'
END
END
AND tl.storenum < 699
ORDER BY tl.StoreNum
First off, the CASE statement must be part of the expression, not the expression itself.
In other words, you can have:
WHERE co.DTEntered = CASE
WHEN LEN('blah') = 0
THEN co.DTEntered
ELSE '2011-01-01'
END
But it won't work the way you have written them eg:
WHERE
CASE LEN('TestPerson')
WHEN 0 THEN co.personentered = co.personentered
ELSE co.personentered LIKE '%TestPerson'
END
You may have better luck using combined OR statements like this:
WHERE (
(LEN('TestPerson') = 0
AND co.personentered = co.personentered
)
OR
(LEN('TestPerson') <> 0
AND co.personentered LIKE '%TestPerson')
)
Although, either way I'm not sure how great of a query plan you'll get. These types of shenanigans in a WHERE clause will often prevent the query optimizer from utilizing indexes.
Try the following:
SELECT * FROM emp_master
WHERE emp_last_name=
CASE emp_first_name
WHEN 'test' THEN 'test'
WHEN 'Mr name' THEN 'name'
END
This should solve your problem for the time being but I must remind you it isn't a good approach :
WHERE
CASE LEN('TestPerson')
WHEN 0 THEN
CASE WHEN co.personentered = co.personentered THEN 1 ELSE 0 END
ELSE
CASE WHEN co.personentered LIKE '%TestPerson' THEN 1 ELSE 0 END
END = 1
AND cc.ccnum = CASE LEN('TestFFNum')
WHEN 0 THEN cc.ccnum
ELSE 'TestFFNum'
END
AND CASE LEN('2011-01-09 11:56:29.327')
WHEN 0 THEN CASE WHEN co.DTEntered = co.DTEntered THEN 1 ELSE 0 END
ELSE
CASE LEN('2012-01-09 11:56:29.327')
WHEN 0 THEN
CASE WHEN co.DTEntered >= '2011-01-09 11:56:29.327' THEN 1 ELSE 0 END
ELSE
CASE WHEN co.DTEntered BETWEEN '2011-01-09 11:56:29.327'
AND '2012-01-09 11:56:29.327'
THEN 1 ELSE 0 END
END
END = 1
AND tl.storenum < 699
I think that the beginning of your query should look like that:
SELECT
tl.storenum [Store #],
co.ccnum [FuelFirst Card #],
co.dtentered [Date Entered],
CASE st.reasonid
WHEN 1 THEN 'Active'
WHEN 2 THEN 'Not Active'
WHEN 0 THEN st.ccstatustypename
ELSE 'Unknown'
END [Status],
CASE st.ccstatustypename
WHEN 'Active' THEN ' '
WHEN 'Not Active' THEN ' '
ELSE st.ccstatustypename
END [Reason],
UPPER(REPLACE(REPLACE(co.personentered,'RT\\\\',''),'RACETRAC\\\\','')) [Person Entered],
co.comments [Comments or Notes]
FROM comments co
INNER JOIN cards cc ON co.ccnum=cc.ccnum
INNER JOIN customerinfo ci ON cc.customerinfoid=ci.customerinfoid
INNER JOIN ccstatustype st ON st.ccstatustypeid=cc.ccstatustypeid
INNER JOIN customerstatus cs ON cs.customerstatuscd=ci.customerstatuscd
INNER JOIN transactionlog tl ON tl.transactionlogid=co.transactionlogid
LEFT JOIN stores s ON s.StoreNum = tl.StoreNum
WHERE
CASE
WHEN (LEN([TestPerson]) = 0 AND co.personentered = co.personentered) OR (LEN([TestPerson]) <> 0 AND co.personentered LIKE '%'+TestPerson) THEN 1
ELSE 0
END = 1
AND
BUT
what is in the tail is completely not understandable
There WHERE part could be written like this:
WHERE
(LEN('TestPerson') <> 0 OR co.personentered = co.personentered) AND
(LEN('TestPerson') = 0 OR co.personentered LIKE '%TestPerson') AND
(cc.ccnum = CASE LEN('TestFFNum')
WHEN 0 THEN cc.ccnum
ELSE 'TestFFNum'
END ) AND
(LEN('2011-01-09 11:56:29.327') <> 0 OR co.DTEntered = co.DTEntered ) AND
((LEN('2011-01-09 11:56:29.327') = 0 AND LEN('2012-01-09 11:56:29.327') <> 0) OR co.DTEntered >= '2011-01-09 11:56:29.327' ) AND
((LEN('2011-01-09 11:56:29.327') = 0 AND LEN('2012-01-09 11:56:29.327') = 0) OR co.DTEntered BETWEEN '2011-01-09 11:56:29.327' AND '2012-01-09 11:56:29.327' ) AND
tl.storenum < 699
select
d.DISTNAME,e.BLKNAME,a.childid,a.studyingclass
from Tbl_AdmissionRegister a
inner join District_master b on a.Schooid=b.Schooid
where
case when len('3601')=4 then c.distcd
when len('3601')=6 then c.blkcd
when len('3601')=11 then c.schcd end = '3601'
Thanks for this question, actually I am looking for something else which is in below query. this may helps someone.
SELECT DISTINCT CASE WHEN OPPORTUNITY='' THEN '(BLANK)' ELSE OPPORTUNITY END
AS OPP,LEN(OPPORTUNITY) FROM [DBO].[TBL]
above query is to fill in dropdown which blank values shows as "(blank)". Also if we pass this value into sql where clause to get blank values with other values I don't know how to handle that. And finally came up with below solution this may helps to somebody.
here it is ,
DECLARE #OPP TABLE (OPP VARCHAR(100))
INSERT INTO #OPP VALUES('(BLANK)'),('UNFUNDED'),('FUNDED/NOT COMMITTED')
SELECT DISTINCT [OPPORTUNITY]
FROM [DBO].[TBL] WHERE ( CASE WHEN OPPORTUNITY ='' THEN '(BLANK)' ELSE OPPORTUNITY END IN (SELECT OPP FROM #OPP))
ORDER BY 1
You could also try like below eg. to show only Outbound Shipments
SELECT shp_awb_no,shpr_ctry_cd, recvr_ctry_cd,
CASE WHEN shpr_ctry_cd = record_ctry_cd
THEN "O"
ELSE "I"
END AS route
FROM shipment_details
WHERE record_ctry_cd = "JP"
AND "O" = CASE WHEN shpr_ctry_cd = record_ctry_cd
THEN "O"
ELSE "I"
END
CASE LEN('TestPerson')
WHEN 0 THEN co.personentered = co.personentered ELSE co.personentered LIKE '%TestPerson'
Try the following:
... and (
(LEN('TestPerson') = 0 and co.personentered = co.personentered) or
(LEN('TestPerson') <> 0 and co.personentered LIKE '%TestPerson') ) and ...
here is my solution
AND CLI.PE_NOM Like '%' + ISNULL(#NomClient, CLI.PE_NOM) + '%'
Regads
Davy
This works
declare #v int=A
select * from Table_Name where XYZ=202
and
dbkey=(case #v when A then 'Some Value 1'
else 'Some Value 2'
end)
I'm using somtehing like that to filter users according to needle as string and a dropdown in the UI
where
u.username like '%' + isnull(#needle, '') + '%'
and 1 =
(
case
when #status = 0 then 1 -- All in uu
when #status = u.statusid then 1 -- Special status
else 0 -- otherwise reject
end
)
select TUM1.userid,TUM1.first_name + ' ' +TUM1.last_name as NAME,tum1.Business_Title,TUM1.manager_id,tum2.First_Name + ' ' + tum2.Last_Name as [MANAGER NAME],TUM1.project,TUM1.project_code,TUM1.rcc_code,TUM1.department,TCM.Company_Name,
case
when tum1.Gender_ID=1 then 'male'
else 'female'
end 'GENDER'
,tum1.Band as BAND,
case when tum1.Inactive=0 then 'STILL IN COMPANY'
else 'LEFT COMPANY'
end 'ACTIVE/INACTIVE'
from tbl_user_master TUM1
join tbl_Company_Master TCM on TCM.Company_Code=TUM1.Company_Code
join tbl_User_Master TUM2 on TUM1.Manager_ID=TUM2.UserID
where tum1.UserID in ('54545414')
SELECT * from TABLE
WHERE 1 = CASE when TABLE.col = 100 then 1
when TABLE.col = 200 then 2 else 3 END
and TABLE.col2 = 'myname';
Use in this way.