How to modify multiple CASE statements - sql-server

It's me again. I would just like to ask your opinion on how shall I modify my SELECT statament query. So basically I created a user defined field called "Force Schedule2", if this checkbox is tick (equals 1), then even if the item is on schedule 1, it will display on schedule 2. How shall I add it on my existing case statement?
my current SELECT statement is:
SELECT
CASE
WHEN
WorkOrder.DateCreated <
(
CASE
WHEN
(DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2)
THEN
(ToBeScheduled_InProgress.Start + 0.625) - 3
ELSE
(ToBeScheduled_InProgress.Start + 0.625) - 1
END
)
THEN
1
ELSE
2
END
AS ScheduleTime
and my user defined field for "Force Schedule2 is:
dbo.AdditionalInfo.UserDefined3 AS ForceSched

It's not obvious what you're trying to do here with your date operation. You could do something simple like this:
CASE
WHEN dbo.AdditionalInfo.UserDefined3 = 1
THEN 2
WHEN WorkOrder.DateCreated < (CASE WHEN (DATEPART(dw, dbo.ToBeScheduled_InProgress.Start) = 2) THEN (ToBeScheduled_InProgress.Start + 0.625) - 3 ELSE (ToBeScheduled_InProgress.Start + 0.625) - 1 END)
THEN 1
ELSE 2
END AS ScheduleTime
Basically it hits the condition and exits the statement. If you flipped it so your date operation was first it would return 1, so you still need to think about the order you're entering things into case statement.

Related

Snowflake with regular expression

Trying to run the below SQL in Snowflake:
SELECT fm_id ,
CASE
WHEN regexp_instr(ASSD,'...',1) > 0
THEN regexp_SUBSTR(ASSD,1,regexp_instr(ASSD,'...',1)-1)
ELSE ASSD
END ASSD
from
(SELECT a.fm_id,
listagg(a.STUID, '; ') within GROUP (
ORDER BY a.Fm_id, a.STUID ) ASSD
from stu_d a
where fm_id = 1222
group by a.fm_id
)
Getting error:
"Invalid parameter value: 0. Reason: Position must be positive"
seems it is failing at -1 or 0 value in above case statement.
What am I doing wrong?
Without seeing your table it's hard to say, but regexp_instr() will return 1 when the pattern is at the beginning of the string. Then, you subtract 1, and 0 is an invalid position argument to regexp_substr(). doc
Perhaps you intended to use SUBSTR not REGEX_SUBSTR()

Achieve where condition and case statement if null

Below is my where condition in query
WHERE ResetID= CASE WHEN NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item) THEN NULL
WHEN #LastIssued < #LastDispatched THEN 1
WHEN #LastIssued >= #LastDispatched THEN 2
END
If case statement returns null the where condition will not work.. Any alternate solution for this condition without case
If you want to have NULL = NULL, you need to handle it with IS NULL. At a guess, what you want is this:
WHERE (ResetID = CASE WHEN NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item) THEN NULL
WHEN #LastIssued < #LastDispatched THEN 1
WHEN #LastIssued >= #LastDispatched THEN 2
END
OR (ResetID IS NULL AND NOT EXISTS (select 1 FROM ops.table where LabelItemId=#Item)))
Edit note that this does require 2 scans of the table ops.table; there may be a better way to do this, but difficult to suggest when we only have a snippet.

Using results of a SELECT to pull relevant values from another table

I'm parsing a string from a table with the following query.
SELECT
CASE
WHEN jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jobdata,
CHARINDEX('DefColId=', jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jobdata) - CHARINDEX('DefColId', jobdata) - 9)
ELSE 'No Import Library'
END AS 'Import_Library'
FROM
jobscompleted
What I want to do then is to take each row and pull another value via another table. I know I could just write out the same statement twice, as in,
SELECT
CASE
WHEN jc.jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jc.jobdata,
CHARINDEX('DefColId=', jc.jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jc.jobdata) - CHARINDEX('DefColId', jc.jobdata) - 9)
ELSE NULL
END AS 'Import_Library',
c.collectionname
FROM
jobscompleted jc
JOIN
collection c ON CASE
WHEN jc.jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jc.jobdata,
CHARINDEX('DefColId=', jc.jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jc.jobdata) - CHARINDEX('DefColId', jc.jobdata) - 9)
ELSE NULL
END = c.collectionid
but that obviously is super redundant. Is there a way to do this in a much more succinct and readable way, and bear in mind I don't really know anything about stored procedures or variables (although there's always a good time to learn).
Not sure if this is what you are asking for. If not, please provide more details to the question.
SELECT * FROM
(SELECT
CASE
WHEN jobdata LIKE '%DocFtpPassword%'
THEN SUBSTRING(jobdata,
CHARINDEX('DefColId=', jobdata) + 9,
CHARINDEX('|DocFtpPassword=', jobdata) - CHARINDEX('DefColId', jobdata) - 9)
ELSE 'No Import Library'
END AS 'Import_Library'
FROM
jobscompleted ) AS jc
INNER JOIN
collection c ON jc.Import_Library = c.collectionid

Select Records Expert equivalent for SSRS

Crystal Reports has the Select Records Expert which allows you to use variables to build WHERE conditions. Is there something similar to the select records experts in SSRS?
If the variable #Restriction below is equal to one, the column name CFE_EDI.ETAT = 'ENV' or CFE_EDI.ETAT = 'OUV'. I tried to mimic the functionality with a SQL query but due to AND and OR precedence, the WHERE condition is not functioning properly.
You can handle this within the SQL of your SSRS Dataset where clause buy using case statements that return a 1 or a 0 depending on whether or not the criteria is met:
where case when #Restriction = '1'
then case when CFE_EDI.ETAT in('ENV','OUV') then 1 else 0 end
when #Restriction = '0'
then case when CFE_EDI.ETAT not in('ENV','OUV') then 1 else 0 end
else case when CFE_EDI.ETAT <> '' then 1 else 0 end
end = 1

case when but two columns instead of one column

I need some advice and suggestions if there is available some way of doing something like this:
SELECT Status=Case
WHEN clflf.product='active'
THEN 'Active' ELSE 'NotActive' END;
this basically creates active or notactive in one column.
Is there some way of doing a case when or some other technique so that I can have two columns, 'Active' and 'NotActive'?
You need separate case statements, like so.
Select StatusActive = Case when clflf.product='active' then 1 ELSE 0 end
,StatusNotActive = Case when clflf.product <>'active' then 1 ELSE 0 end
You can always do
SELECT active,
1 - active AS notactive
FROM clflf
CROSS APPLY (SELECT CASE
WHEN clflf.product = 'active' THEN 1
ELSE 0
END) CA(active)

Resources