How can I use case when .. then in #Parameters in MS Report Builder?
For example this code does not work correct, because Filter does not work
SELECT DISTINCT case
when [APPROVAL_STATUS] = 'approved' then '1'
when [APPROVAL_STATUS] = 'denied' then '2'
when [APPROVAL_STATUS] = 'pending' then '3'
when isnull([APPROVAL_STATUS], ' ') = '' then '4'
ELSE [APPROVAL_STATUS]
end as [APPROVAL_STATUS]
FROM [dbo].[SUMMARY]
Is it possible?
There is a query in which I have to concatenate 12 columns in a case statement. It would've been easier if it was 2 or 3. To be clearer, here is my query:
SELECT column1
,column2
,effet_secondaire_desc = CASE WHEN [symp01] = 'y' THEN 'Pedi apeti'
WHEN [symp02] = 'y' THEN 'Ke plen'
WHEN [symp03] = 'y' THEN 'Zye jon'
WHEN [symp04] = 'y' THEN 'Dyare'
WHEN [symp05] = 'y' THEN 'Ko grate'
WHEN [symp06] = 'y' THEN 'Lafyev ak frison'
WHEN [symp07] = 'y' THEN 'Ko fe mal ak jwenti fe mal'
WHEN [symp08] = 'y' THEN 'Pikotman'
WHEN [symp09] = 'y' THEN 'Fatig'
WHEN [symp10] = 'y' THEN 'Tet vire/vetij'
WHEN [symp11] = 'y' THEN 'Vant fe mal'
WHEN [symp12] = 'y' THEN [symp12_desc]
ELSE '~'
END
.....
,columnx
FROM MyTable
[symp01] ... [symp12] are the 12 columns I want to concatenate. So the value of effet_secondaire_desc should be something like this 'Pedi apeti, Ko grate, Lafyev ak frison, ...'.
Can anyone help me with this please?
Thanks in advance.
select
column1
, column2
, effet_secondaire_desc = isnull(nullif(stuff(
case when [symp01] = 'y' then ', Pedi apeti' else '' end
+case when [symp02] = 'y' then ', Ke plen' else '' end
+case when [symp03] = 'y' then ', Zye jon' else '' end
+case when [symp04] = 'y' then ', Dyare' else '' end
+case when [symp05] = 'y' then ', Ko grate' else '' end
+case when [symp06] = 'y' then ', Lafyev ak frison' else '' end
+case when [symp07] = 'y' then ', Ko fe mal ak jwenti fe mal' else '' end
+case when [symp08] = 'y' then ', Pikotman' else '' end
+case when [symp09] = 'y' then ', Fatig' else '' end
+case when [symp10] = 'y' then ', Tet vire/vetij' else '' end
+case when [symp11] = 'y' then ', Vant fe mal' else '' end
+case when [symp12] = 'y' then ', '+[symp12_desc] else '' end
,1,2,''),''),'~')
, columnx
from mytable
So something like this?:
SELECT
...
CASE WHEN [symp01] = 'y' THEN ', Pedi apeti' ELSE '' END
+ CASE WHEN [symp02] = 'y' THEN ', Ke plen' ELSE '' END
+ ...
END
And then wrap that in some logic to remove the first comma and space.
I am building a SSRS Report to show Student Class Information.
I have a table named class with information regarding classes students were attended.
My requirement is to show the days in which the classes occur. For eg: If Column Name Monday = 'Y' then I have to show the day as [Mon]. I have tried below code .
SELECT
case when Monday ='Y' then'[Mon]' else '' end +case when Monday ='Y' then ',' else '' end +
case when Tuesday ='Y' then'[Tue]' else '' end +case when Tuesday ='Y' then ','else '' end +
case when Wednesday='Y' then'[Wed]' else '' end +case when Wednesday ='Y' then ',' else '' end +
case when Thursday ='Y' then'[Thu]' else '' end +case when Thursday ='Y' then ',' else '' end +
case when Friday ='Y' then'[Fri]' else '' end +case when Friday ='Y' then ',' else '' end +
case when Saturday ='Y' then'[Sat]' else '' end +case when Saturday ='Y' then ',' else '' end +
case when Sunday ='Y' then'[Sun]' else '' end as classday
FROM vw_Class_Without_Instructor
I got the output as below.
In output there is an extra comma at the end of each line. Any way to exclude it in the select statement itself?. Is there any other way to get the output?. Use of case statements seems to be slow down the execution.
Please try this below query
SELECT
SUBSTRING(case when Monday ='Y' then',[Mon]' else '' end +
case when Tuesday ='Y' then',[Tue]' else '' end +
case when Wednesday='Y' then',[Wed]' else '' end +
case when Thursday ='Y' then',[Thu]' else '' end +
case when Friday ='Y' then',[Fri]' else '' end +
case when Saturday ='Y' then',[Sat]' else '' end +
case when Sunday ='Y' then',[Sun]' else '' end, 2,200) as classday
FROM vw_Class_Without_Instructor
trick: I've added commas with text so no half number of case evaluations. With ,[Mon] value I've ensured that there is always a comma in beginning which is removed using SUBSTRING.
be wise to choose a very high value in last part parameter of SUBSTRING so that it return everything starting from the charindex in second part.
See msdn reference: https://msdn.microsoft.com/en-us/library/ms187748.aspx
If the sum of start and length is greater than the number of
characters in expression, the whole value expression beginning at
start is returned.
You can try this;
select
CASE WHEN LEN(classday_with_comma) > 0 THEN LEFT(classday_with_comma,LEN(classday_with_comma) - 1) ELSE '' END as classday
From (
SELECT
case when Monday = 'Y' then '[Mon],' else '' end +
case when Tuesday = 'Y' then '[Tue],' else '' end +
case when Wednesday = 'Y' then '[Wed],' else '' end +
case when Thursday = 'Y' then '[Thu],' else '' end +
case when Friday = 'Y' then '[Fri],' else '' end +
case when Saturday = 'Y' then '[Sat],' else '' end +
case when Sunday = 'Y' then '[Sun],' else '' end as classday_with_comma
FROM vw_Class_Without_Instructor
) as x
Two case statement can be truncated to one from
case when Monday ='Y' then'[Mon]' else '' end +
case when Monday ='Y' then ',' else '' end +
To
case when Monday = 'Y' then '[Mon],' else '' end +
And
LEFT(classday_with_comma, LEN(classday_with_comma) - 1)
will remove the last ,
I am using following SQL query to find difference between two datetime fields which is calculate night time shift,
For example
today work time is evening 190957 til next date morning 071152 so the between time in fab minute is 628, but out fab time will be calculate 798 min(Over calculate, more than IN fab time). so i want calculate in accurate minute.
SELECT
GROUP_SCANTIMECAL.EP_EMP_ID
,SUM(CAST(GROUP_SCANTIMECAL.INFAB_MIN AS FLOAT)) AS INFAB
,SUM(CAST(GROUP_SCANTIMECAL.OUTFAB_MIN AS FLOAT)) AS OUTFAB
FROM
(
SELECT SCANHIST.EP_SCAN_DATE
,SCANHIST.EP_EMP_ID
,SCANHIST.EP_EMP_NAME
,SCANHIST.EP_EMP_DEPT
,SCANHIST.EP_EMP_SECTION
,SCANHIST.EP_EMP_SHIFT
,SCANHIST.EP_TRANS_LOC
,DATEDIFF(MI,CAST(STUFF(STUFF(SCANHIST.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME),
CAST(STUFF(STUFF(NEXTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) AS INFAB_MIN
,DATEDIFF(MI,CAST(STUFF(STUFF(NEXTSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME),
CAST(STUFF(STUFF(PREVSCAN.EP_SCAN_DATE,12,0,':'),15,0,':') AS DATETIME)) AS OUTFAB_MIN
FROM [AcmkIMS].[dbo].[EP_SCAN_HIST] SCANHIST
OUTER APPLY
(
SELECT TOP 1
NEXTSCAN.EP_SCAN_DATE
,NEXTSCAN.EP_EMP_ID
,NEXTSCAN.EP_EMP_NAME
,NEXTSCAN.EP_EMP_DEPT
,NEXTSCAN.EP_EMP_SECTION
,NEXTSCAN.EP_EMP_SHIFT
,NEXTSCAN.EP_SCAN_ID
,NEXTSCAN.EP_TRANS_DESC
,NEXTSCAN.EP_TRANS_LOC
FROM [AcmkIMS].[dbo].[EP_SCAN_HIST] NEXTSCAN
JOIN [AcmkIMS].[dbo].[EP_EMP_INFO] EMPINFO
ON EMPINFO.EP_EMP_ID = NEXTSCAN.EP_EMP_ID
JOIN [AcmkIMS].[dbo].[EP_SHIFT_CALENDAR] SHIFTCAL
ON SHIFTCAL.EP_SHIFT_NAME = NEXTSCAN.EP_EMP_SHIFT
AND SHIFTCAL.EP_SHIFT_DATE = LEFT(FIRSTSCAN.EP_SCAN_DATE,8)
JOIN [AcmkIMS].[dbo].[EP_SHIFT_DESC] SHIFTDESC
ON SHIFTDESC.EP_SHIFT_NAME = SHIFTCAL.EP_SHIFT
WHERE 1=1
AND SCANHIST.EP_SCAN_ID = NEXTSCAN.EP_SCAN_ID
AND NEXTSCAN.EP_SCAN_DATE > SCANHIST.EP_SCAN_DATE
AND NEXTSCAN.EP_SCAN_DATE < CASE WHEN (FIRSTSCAN.EP_SHIFT = 'N1')
THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,LEFT(NEXTSCAN.EP_SCAN_DATE,8)),112) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
ELSE CASE WHEN (FIRSTSCAN.EP_SHIFT = 'R1' OR FIRSTSCAN.EP_SHIFT = 'R2')
THEN
LEFT(NEXTSCAN.EP_SCAN_DATE,8) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
ELSE
LEFT(NEXTSCAN.EP_SCAN_DATE,8) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END END
AND NEXTSCAN.EP_TRANS_LOC = 'OUT'
ORDER BY NEXTSCAN.EP_SCAN_DATE
)NEXTSCAN
OUTER APPLY
(
SELECT TOP 1
PREVSCAN.EP_SCAN_DATE
,PREVSCAN.EP_EMP_ID
,PREVSCAN.EP_EMP_NAME
,PREVSCAN.EP_EMP_DEPT
,PREVSCAN.EP_EMP_SECTION
,PREVSCAN.EP_EMP_SHIFT
,PREVSCAN.EP_SCAN_ID
,PREVSCAN.EP_TRANS_DESC
,PREVSCAN.EP_TRANS_LOC
FROM [AcmkIMS].[dbo].[EP_SCAN_HIST] PREVSCAN
JOIN [AcmkIMS].[dbo].[EP_EMP_INFO] EMPINFO
ON EMPINFO.EP_EMP_ID = PREVSCAN.EP_EMP_ID
JOIN [AcmkIMS].[dbo].[EP_SHIFT_CALENDAR] SHIFTCAL
ON SHIFTCAL.EP_SHIFT_NAME = PREVSCAN.EP_EMP_SHIFT
AND SHIFTCAL.EP_SHIFT_DATE = LEFT(FIRSTSCAN.EP_SCAN_DATE,8)
JOIN [AcmkIMS].[dbo].[EP_SHIFT_DESC] SHIFTDESC
ON SHIFTDESC.EP_SHIFT_NAME = SHIFTCAL.EP_SHIFT
WHERE 1=1
AND SCANHIST.EP_SCAN_ID = PREVSCAN.EP_SCAN_ID
AND PREVSCAN.EP_SCAN_DATE > SCANHIST.EP_SCAN_DATE
AND PREVSCAN.EP_SCAN_DATE < CASE WHEN (FIRSTSCAN.EP_SHIFT = 'N1')
THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,LEFT(SCANHIST.EP_SCAN_DATE,8)),112) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
ELSE CASE WHEN (FIRSTSCAN.EP_SHIFT = 'R1' OR FIRSTSCAN.EP_SHIFT = 'R2')
THEN
LEFT(SCANHIST.EP_SCAN_DATE,8) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
ELSE
LEFT(SCANHIST.EP_SCAN_DATE,8) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END END
AND PREVSCAN.EP_TRANS_LOC = 'IN'
ORDER BY PREVSCAN.EP_SCAN_DATE
)PREVSCAN
JOIN [AcmkIMS].[dbo].[EP_EMP_INFO] EMPINFO
ON EMPINFO.EP_EMP_ID = SCANHIST.EP_EMP_ID
JOIN [AcmkIMS].[dbo].[EP_SHIFT_CALENDAR] SHIFTCAL
ON SHIFTCAL.EP_SHIFT_NAME = SCANHIST.EP_EMP_SHIFT
AND SHIFTCAL.EP_SHIFT_DATE = LEFT(FIRSTSCAN.EP_SCAN_DATE,8)
JOIN [AcmkIMS].[dbo].[EP_SHIFT_DESC] SHIFTDESC
ON SHIFTDESC.EP_SHIFT_NAME = SHIFTCAL.EP_SHIFT
WHERE 1=1
AND SCANHIST.EP_SCAN_DATE >= FIRSTSCAN.EP_SCAN_DATE
AND SCANHIST.EP_SCAN_DATE < CASE WHEN (FIRSTSCAN.EP_SHIFT = 'N1')
THEN CONVERT(VARCHAR(8),DATEADD(DAY,+1,LEFT(FIRSTSCAN.EP_SCAN_DATE,8)),112) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') ELSE
CASE WHEN (FIRSTSCAN.EP_SHIFT = 'R1' OR FIRSTSCAN.EP_SHIFT = 'R2')
THEN
LEFT(FIRSTSCAN.EP_SCAN_DATE,8) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+0,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','')
ELSE
LEFT(FIRSTSCAN.EP_SCAN_DATE,8) + ' ' +
REPLACE(CONVERT(VARCHAR(8),DATEADD(HOUR,+6,SHIFTDESC.EP_SHIFT_TIMETO + ':00'),108),':','') END END
AND SCANHIST.EP_EMP_ID = FIRSTSCAN.EP_EMP_ID
AND SCANHIST.EP_TRANS_LOC = 'IN'
)GROUP_SCANTIMECAL
GROUP BY GROUP_SCANTIMECAL.EP_EMP_ID
)SCANTIMECAL
Why is it that I can't do the below script and how would I go about fixing it? I get the error Inccorect syntax near '='. I was doing it using ...= 1 THEN 'D' ELSE '' END as Aset but I need to check Aset and Bset in the third Case so I declared Aset and Bset instead.
declare #Aset varchar(10)
declare #Bset varchar(10)
UPDATE m
SET m.new_name = m.new_name + ' ' + #ASet + AndSet + #BSet + ' Type'
FROM contactMaster m
inner join contact c on
m.contactid = c.contactid
CROSS APPLY (
SELECT CASE #Aset WHEN (c.category1|
c.category2|
c.category3|
c.category4) = 1 THEN 'C' ELSE '' End
,CASE #Bset WHEN (c.category5|
c.category6|
c.category7|
c.category8) = 1 THEN 'D' ELSE '' END
,CASE WHEN #BSet = 'D' and #ASet = 'C' THEN ' & ' ELSE '' END AS AndSet
) AS CA1
I don't have your table schema or data, so I had to wing it and couldn't test it, but see if this might work:
UPDATE m
SET m.new_name = m.new_name
+ ' '
+ CA1.ASet
+ CASE WHEN
(
CA1.Aset = 'C' AND
CA1.Bset = 'D'
)
THEN ' & ' ELSE '' END
+ CA1.BSet + ' Type'
FROM contactMaster m
JOIN contact c on m.contactid = c.contactid
CROSS APPLY (
SELECT CASE WHEN
(
c.category1 |
c.category2 |
c.category3 |
c.category4
)
= 1 THEN 'C' ELSE '' END AS Aset,
CASE WHEN
(
c.category5 |
c.category6 |
c.category7 |
c.category8
)
= 1 THEN 'D' ELSE '' END AS Bset
) AS CA1