Can someone help me out with the correct syntax for the below CASE statement please? It's part of a view I am creating where the Return Price is half the cost of the calculated cost in the jobs table when the charge description has "Return" in it. its saying incorrect syntax the the keyword ELSE. Thanks in advance.
`
/****** Object: View [dbo].[MMXREPORT] Script Date: 11/08/2016 14:06:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter view [dbo].[MMXREPORT] AS
DECLARE #output int
SELECT isnull(Cus.CUSTOMERNUMBER,'') AS "ACCOUNT",
isnull(dbo.Jobs.DELDATEANDTIME,'') as "DELIVERY DATE" ,
isnull(dbo.Jobs.JOBREFERENCE,'') AS "MMX REFERENCE",
isnull(dbo.Jobs.JOBNUMBER,'') AS "INDIGO REF",
isnull(dbo.Jobs.COLPOSTCODE,'') AS "COLLECTION POSTCODE",
isnull(dbo.Jobs.DELTOWN,'') AS "DELIVERY TOWN",
isnull(dbo.Jobs.DELLPOSTCODE,'') AS "DELIVERY POST CODE",
CASE when (select count (*) from jobcharge jobc inner join charge
on charge.chargeid = jobc.chargeid
where jobc.jobid = jobs.jobid and charge.description like '%return%') > 0 THEN 'YES'
ELSE 'NO' END as "RETURN AUTHORISED",
isnull(dbo.GetClarionTime (DELREADYAT ),'') AS "PLANNED DELIVERY TIME",
'' AS "DRIVER ARRIVAL TIME",
'' AS "ENGINEER ARRIVAL TIME",
isnull(convert(char(5), dbo.Jobs.PODDATEANDTIME, 108),'') AS"POD TIME",
isnull(dbo.Jobs.PODNAME,'') AS"POD NAME",
isnull(dbo.Vehicle.VEHICLE,'') AS "VEHICLE SIZE",
isnull(dbo.Jobs.ACTUALMILEAGE,'0') AS "MILEAGE",
isnull(dbo.Jobs.CALCULATEDCOST,'') AS "MILEAGE PRICE",
SELECT #output = count (*) from jobcharge jobc inner join charge
ON charge.chargeid = jobc.chargeid
WHERE jobc.jobid = jobs.jobid AND charge.description LIKE '%return%'
CASE WHEN #output > 0 THEN SUM(JOBS.CALCULATEDCOST/2)
ELSE '0.00' END
AS "RETURN PRICE",
isnull((select sum (dbo.JobCharge.FLATRATE) from jobcharge inner join charge on charge.chargeid = jobcharge.chargeid
WHERE dbo.Charge.DESCRIPTION like '%congestion%' and jobcharge.jobid = jobs.jobid),0) AS "CONGESTION CHARGE",
'' AS "CONGESTION CHARGE COMMENTS",
isnull((select sum (dbo.JobCharge.FLATRATE) from jobcharge inner join charge on charge.chargeid = jobcharge.chargeid
WHERE dbo.Charge.DESCRIPTION like '%waiting time%' and jobcharge.jobid = jobs.jobid),0) AS "WAITING TIME CHARGE",
'' AS "WAITING TIME COMMENTS",
isnull((select sum (dbo.JobCharge.FLATRATE) from jobcharge inner join charge on charge.chargeid = jobcharge.chargeid
WHERE dbo.Charge.DESCRIPTION like '%hrs%' and jobcharge.jobid = jobs.jobid),0) AS "ANY OTHER CHARGES",
'' AS "ANY OTHER COMMENTS",
'' AS "TOTAL COST",JOBS.CUSTOMERID
FROM dbo.Jobs left outer JOIN
dbo.Vehicle ON dbo.Jobs.VEHICLEID = dbo.Vehicle.VEHICLEID left outer JOIN
-- dbo.JobCharge ON dbo.Jobs.JobID = dbo.JobCharge.JOBID left outer JOIN
dbo.Customer CUS ON dbo.Jobs.CUSTOMERID = CUS.CUSTOMERID
`
Above is the full query.
This should work:
DECLARE #output int
select #output= count (*) from jobcharge jobc inner join charge
on charge.chargeid = jobc.chargeid
where jobc.jobid = jobs.jobid and charge.description like '%return%'
CASE WHEN #output > 0 THEN SUM(JOBS.CALCULATEDCOST/2)
ELSE '0.00' END as "RETURN PRICE",
It seems incomplete query.
So i have assumed that there must be a table named "JOBS".
For that pl try following.
SELECT
CASE WHEN (SELECT COUNT (*)FROM JOBCHARGE JOBC
INNER JOIN CHARGE ON CHARGE.CHARGEID = JOBC.CHARGEID
WHERE JOBC.JOBID = JOBS.JOBID
AND CHARGE.DESCRIPTION LIKE '%return%') > 0
THEN
SUM(JOBS.CALCULATEDCOST/2)
ELSE '0.00'
END as "RETURN PRICE" FROM JOBS
I think you do not need the sum of several rows, but only one value ...
CASE WHEN EXISTS(
select 1 from jobcharge jobc inner join charge
ON charge.chargeid = jobc.chargeid
WHERE jobc.jobid = jobs.jobid AND charge.description LIKE '%return%
) THEN jobs.CALCULATEDCOST/2 ELSE 0.00 END as "RETURN PRICE",
Use exists (select 1 ...) may by faster than select count() ...
Related
I have the following SQL query, and I would need it to run SELECT statement only once.
May I know what are the changes I should make?
EDIT: Need to change to use only one SELECT statement..
DECLARE
#lang varchar(2) ='en',
#hideInactiveCompany integer = 0
IF #hideInactiveCompany = 1
SELECT <all columns>
FROM Config.BusinessUnit BU
LEFT JOIN Config.Organization CO on BU.OrganizationId = CO.OrganizationId
LEFT JOIN Config.ParentBusinessUnit PBU on BU.ParentBusinessUnitId = PBU.ParentBusinessUnitId
WHERE BU.IsActive = 1 --the additional condition if #hideInactiveCompany=1
ORDER BY CASE WHEN #lang = 'cn' THEN BU.Lang END,
CASE WHEN #lang = 'en' THEN BU.Lang END DESC,
EntityName
ELSE
SELECT <all columns>
FROM Config.BusinessUnit BU
LEFT JOIN Config.Organization CO on BU.OrganizationId = CO.OrganizationId
LEFT JOIN Config.ParentBusinessUnit PBU on BU.ParentBusinessUnitId = PBU.ParentBusinessUnitId
ORDER BY CASE WHEN #lang = 'cn' THEN BU.Lang END,
CASE WHEN #lang = 'en' THEN BU.Lang END DESC,
EntityName
If #hideInactiveCompany = 1, display output with BU.IsActive =1
If #hideInactiveCompany = 0, display output without BU.IsActive
filter.
We can try writing the following WHERE clause:
WHERE
(hideInactiveCompany = 1 AND BU.IsActive IS NOT NULL) OR
(hideInactiveCompany IS NULL AND BU.IsActive IS NULL) OR
(hideInactiveCompany <> 1)
Note: The OP has since changed the question a few times after I answered. The latest answer appears to be this:
WHERE
(hideInactiveCompany = 1 AND BU.IsActive = 1) OR
hideInactiveCompany IS NULL OR hideInactiveCompany <> 1
If i do understand your requirement correctly, the entire query should have only one single SELECT clause
DECLARE
#lang varchar(2) ='en',
#hideInactiveCompany integer = 0
SELECT <all columns>
FROM Config.BusinessUnit BU
LEFT JOIN Config.Organization CO on BU.OrganizationId = CO.OrganizationId
LEFT JOIN Config.ParentBusinessUnit PBU on BU.ParentBusinessUnitId = PBU.ParentBusinessUnitId
WHERE (#hideInactiveCompany = 1 AND BU.IsActive = 1)
OR (#hideInactiveCompany <> 1)
ORDER BY CASE WHEN #lang = 'cn' THEN BU.Lang END,
CASE WHEN #lang = 'en' THEN BU.Lang END DESC,
EntityName
Explanation on WHERE
Basically there are only 2 conditions:-
Condition 1 : #hideInactiveCompany = 1 AND BU.IsActive = 1
Condition 2 : #hideInactiveCompany <> 1
Condition 1 is as what you specified. When variable #hideInactiveCompany is 1, IsActive must be 1
Condition 2 is when variable #hideInactiveCompany not equal to 1
You cannot do such a "conditionally build my select" in SQL. You need to write the complete select query twice, one for each branch of the if.
(SQL is generally not a "composable" langauge, you cannot freely combine the elements how you want.)
You might be able to use case and other expressions to rebuild in a single query, or doing the common part of the query into a temporary table (or table valued variable) and then only the final part twice.
Use IF ELSE condition:
ALTER PROCEDURE [Config].[usp_ListBusinessUnit]
#lang VARCHAR(2) = NULL,
#hideInactiveCompany INTEGER
AS
BEGIN
IF #hideInactiveCompany=1
SELECT <all columns>
FROM Config.BusinessUnit BU
LEFT JOIN Config.Organization CO ON BU.OrganizationId = CO.OrganizationId
LEFT JOIN Config.ParentBusinessUnit PBU ON BU.ParentBusinessUnitId =
PBU.ParentBusinessUnitId
WHERE BU.IsActive IS NOT NULL
ORDER BY
CASE WHEN #lang = 'cn' THEN BU.Lang END,
CASE WHEN #lang = 'en' THEN BU.Lang END DESC,
EntityName
ELSE
SELECT <all columns>
FROM Config.BusinessUnit BU
LEFT JOIN Config.Organization CO ON BU.OrganizationId = CO.OrganizationId
LEFT JOIN Config.ParentBusinessUnit PBU ON BU.ParentBusinessUnitId =
PBU.ParentBusinessUnitId
WHERE BU.IsActive IS NULL
ORDER BY
CASE WHEN #lang = 'cn' THEN BU.Lang END,
CASE WHEN #lang = 'en' THEN BU.Lang END DESC,
EntityName
END
When retrieving a data set via a select statement, I want to update existing records based on the vales in columns for each retrieved record. I can't use a function to update a record and I can't use a stored procedure in a select statement. I also fount that I cannot circumvent these restrictions by executing a stored procedure from a function.
Any ideas? Following is my current code with a comment where I feel I need to call something that will update the record. This code will be used in SSRS so one solution might be to execute a stored procedure inside the reports custom code but I cant get that to work either.
select sw.SMACTVSEQ as JDE_ActiveRec
,sw.SMCSSEQ as JDE_SalesSeq
,bu.MCRP04 as JDE_DivID
,sw.SMHBMCUS as JDE_CommID
,cm.CMDL01 as JDE_CommunityName
,rtrim(sw.SMMCU) as JDE_LotID
,sw.SMBYR as JDE_BuyerABNo
,JDE_SSID = case
When ab.ABURRF is null then cast(0 as int)
When ab.ABURRF = '' then cast(0 as int)
else cast(ab.ABURRF as int)
end
,ss.Customer_ID as SS_CustID
,ss.Lot_ID as SS_LotID
,ss.Customer_Status as SS_CustStatus
,[dbo].[udf_ConvertJDEdate](sw.SMCDJ) as JDE_DateClosed
,case when ab.ABURRF >0 then 'Manually Update'
else
case when #Update_Mode ='Yes' then
'Yes/Error'
/* **************
Replace 'Yes/Error' with procedure to update
JDE_F0101_ABURRF and return 'Yes' or 'Error'
************** */
else #Update_Mode
end
end as Update_Mode
from [dbo].[crp_F44H501] sw
left outer join [dbo].[crp_F44H101] cm
on cm.[CMHBMCUS]=sw.smhbmcus and cm.[CMCPHASE]=sw.[SMCPHASE]
left outer join [dbo].[crp_F0006] bu
on bu.mcmcu = sw.smmcu
left outer join [dbo].[stg_F0101] ab
on ab.aban8 = sw.SMBYR
left outer join (
select distinct(lot_id)
,customer_ID
,customer_status
from [dbo].[SS_FactDemographic]
) ss
on ltrim(ss.lot_id) = ltrim(sw.SMMCU)
Where sw.smactvseq='1'
and sw.SMBYR > 0
and ab.ABURRF <> ss.Customer_ID
and ss.Customer_Status = 'Buyer'
and (bu.MCRP04 = #Division_ID or #Division_ID ='All')
and (ltrim(sw.SMHBMCUS) = #Community_ID or #Community_ID ='All')
Order by JDE_DivID,JDE_CommID,JDE_LotID
Put the UPDATE query in the TSQL of the report query before your data query. As long as your query is valid and you have permission, SSRS will run TSQL to UPDATE data, use TEMP tables, etc. The SSRS Query Editor may not like everything that you can do so you might need to make your query in SSMS (which I usually do anyways).
I think your query would look something like:
UPDATE sw
SET Update_Mode = CASE WHEN ab.ABURRF >0 THEN 'Manually Update'
ELSE CASE WHEN #Update_Mode ='Yes' THEN 'Yes/Error' ELSE #Update_Mode END
END
FROM [dbo].[crp_F44H501] sw
INNER JOIN [dbo].[stg_F0101] ab on ab.aban8 = sw.SMBYR
select sw.SMACTVSEQ as JDE_ActiveRec
,sw.SMCSSEQ as JDE_SalesSeq
,bu.MCRP04 as JDE_DivID
,sw.SMHBMCUS as JDE_CommID
,cm.CMDL01 as JDE_CommunityName
,rtrim(sw.SMMCU) as JDE_LotID
,sw.SMBYR as JDE_BuyerABNo
,JDE_SSID = case
When ab.ABURRF is null then cast(0 as int)
When ab.ABURRF = '' then cast(0 as int)
else cast(ab.ABURRF as int)
end
,ss.Customer_ID as SS_CustID
,ss.Lot_ID as SS_LotID
,ss.Customer_Status as SS_CustStatus
,[dbo].[udf_ConvertJDEdate](sw.SMCDJ) as JDE_DateClosed
,sw.Update_Mode
from [dbo].[crp_F44H501] sw
left outer join [dbo].[crp_F44H101] cm
on cm.[CMHBMCUS]=sw.smhbmcus and cm.[CMCPHASE]=sw.[SMCPHASE]
left outer join [dbo].[crp_F0006] bu
on bu.mcmcu = sw.smmcu
left outer join [dbo].[stg_F0101] ab
on ab.aban8 = sw.SMBYR
left outer join (
select distinct(lot_id)
,customer_ID
,customer_status
from [dbo].[SS_FactDemographic]
) ss
on ltrim(ss.lot_id) = ltrim(sw.SMMCU)
Where sw.smactvseq='1'
and sw.SMBYR > 0
and ab.ABURRF <> ss.Customer_ID
and ss.Customer_Status = 'Buyer'
and (bu.MCRP04 = #Division_ID or #Division_ID ='All')
and (ltrim(sw.SMHBMCUS) = #Community_ID or #Community_ID ='All')
Order by JDE_DivID,JDE_CommID,JDE_LotID
Would this work for you:
update X set Update_Mode = case when X.ABURRF > 0 then 'Manually Update' else
case when #Update_Mode = '' then 'Yes/Error' else #Update_Mode
end
end
from
(
select
sw.SMACTVSEQ as JDE_ActiveRec
, sw.SMCSSEQ as JDE_SalesSeq
, bu.MCRP04 as JDE_DivID
, sw.SMHBMCUS as JDE_CommID
, cm.CMDL01 as JDE_CommunityName
, rtrim(sw.SMMCU) as JDE_LotID
, sw.SMBYR as JDE_BuyerABNo
, JDE_SSID = case
When ab.ABURRF is null then cast(0 as int)
When ab.ABURRF = '' then cast(0 as int)
else cast(ab.ABURRF as int)
end
, ss.Customer_ID as SS_CustID
, ss.Lot_ID as SS_LotID
, ss.Customer_Status as SS_CustStatus
, [dbo].[udf_ConvertJDEdate](sw.SMCDJ) as JDE_DateClosed
, ab.ABURRF
, '' as Update_Mode
--, case when ab.ABURRF >0 then 'Manually Update'
-- else
-- case when #Update_Mode ='Yes' then
-- 'Yes/Error'
-- /* **************
-- Replace 'Yes/Error' with procedure to update
-- JDE_F0101_ABURRF and return 'Yes' or 'Error'
-- ************** */
-- else #Update_Mode
-- end
-- end as Update_Mode
from [dbo].[crp_F44H501] sw
left outer join [dbo].[crp_F44H101] cm on cm.[CMHBMCUS]=sw.smhbmcus and cm.[CMCPHASE]=sw.[SMCPHASE]
left outer join [dbo].[crp_F0006] bu on bu.mcmcu = sw.smmcu
left outer join [dbo].[stg_F0101] ab on ab.aban8 = sw.SMBYR
left outer join (select distinct(lot_id),customer_ID,customer_status from [dbo].[SS_FactDemographic]) ss on ltrim(ss.lot_id) = ltrim(sw.SMMCU)
Where sw.smactvseq='1'
and sw.SMBYR > 0
and ab.ABURRF <> ss.Customer_ID
and ss.Customer_Status = 'Buyer'
and (bu.MCRP04 = #Division_ID or #Division_ID ='All')
and (ltrim(sw.SMHBMCUS) = #Community_ID or #Community_ID ='All')
) X
--Order by JDE_DivID
--, JDE_CommID
--, JDE_LotID
Remember, the #Update_Mode variable has to be declared before running this update, else you will get an error.
I have an application which allows users to contact one another, look at it as a dating site. I'm adding some security around the contact part, whereby the user can limit who can contact him/her either by setting a certain age / height range and gender.
Say for example I click on UserId-2 profiles, this will then make a call to the DB, pass in the ID or the chosen profile and also pass my ID in to, I then call my function passing in both of these values. Function is as follows:
ALTER FUNCTION [dbo].[Can_User_Contact]
(
#UserId1 int = 2, -- UserId
#UserId2 int = 1 -- Requested by Id
)
RETURNS bit
AS
BEGIN
RETURN (
SELECT CASE WHEN up.Id IS NULL THEN 0 ELSE 1 END AS does_data_match
FROM [user].User_Settings us
LEFT OUTER JOIN [User].[User_Profile] up ON us.UserId = #UserId1
LEFT OUTER JOIN [User].[User_Details] d ON up.Id = d.UserId
AND up.id = #UserId2
AND d.Height between ISNULL(us.HeightFrom, d.height) and ISNULL(us.HeightTo, d.Height)
AND up.Age between ISNULL(us.AgeFrom, up.age) and ISNULL(us.AgeTo, up.Age)
AND up.Gender = ISNULL(us.Gender, up.Gender)
)
END
Inside this function I compare the values UserId-2 has saved in the settings table to the values of my self by joining on the user profile table and the user details table, the problem with this is every time I execute it I get the following message:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I have two records in the setting table as shown here:
I'm only interested in the second record.
Can someone shed some light into what I might be doing wrong?
#UserId1 is the profile Id,
#UserId2 is the user who's requesting to view it.
Bare in mind, there may not be a record in the settings table for #UserId1 as well as this functionality is optional.
Assuming you have a single entry for user_setting,User_Profile and User_Details for each user, this should work.
Based on your result, it looks like a problem with your join conditions. Some of your join conditions should ideally be part of WHERE clause like up.id = #UserId2.
Query
SELECT TOP 1 CASE WHEN us.UserId IS NULL THEN 0 ELSE 1 END AS does_data_match
FROM [User].[User_Profile] up
INNER JOIN [User].[User_Details] d ON up.Id = d.UserId
LEFT OUTER JOIN [user].User_Settings us
ON us.UserId = #UserId1
AND d.Height between ISNULL(us.HeightFrom, d.height) and ISNULL(us.HeightTo, d.Height)
AND up.Age between ISNULL(us.AgeFrom, up.age) and ISNULL(us.AgeTo, up.Age)
AND up.Gender = ISNULL(us.Gender, up.Gender)
WHERE up.id = #UserId2
ORDER BY up.id ASC
Note: If assumption is true, then ideally TOP is not required. Just added it for additional safety
multiple rows are being returned by SELECT query.
Since you are interested in a single value use like this:
ALTER FUNCTION [dbo].[Can_User_Contact]
(
#UserId1 int = 2, -- UserId
#UserId2 int = 1 -- Requested by Id
)
RETURNS bit
AS
BEGIN
RETURN
(
SELECT CASE WHEN EXISTS
(
SELECT 1 FROM [user].User_Settings us1
LEFT OUTER JOIN [User].[User_Profile] up ON up.id = #UserId2
LEFT OUTER JOIN [User].[User_Details] d ON up.Id = d.UserId
LEFT OUTER JOIN [user].[User_Settings] us2 ON us2.UserId=up.id
WHERE
d.Height between ISNULL(us1.HeightFrom, d.height) and ISNULL(us1.HeightTo, d.Height)
AND us2.Age between ISNULL(us1.AgeFrom, up.age) and ISNULL(us1.AgeTo, up.Age)
AND us1.Gender = ISNULL(us2.Gender, up.Gender)
AND up.Id IS NOT NULL
AND us1.UserId = #UserId1
) THEN 1 ELSE 0 END AS does_data_match
END
Trying to get the select case statement to control whether nothing happens or a table is updated based on the sum of three fields from two different tables. One of the tables is a temp table (#tempGLsum). This holds the id field and the sum amount. The "amt" field in the tblPcardGL table should never go below 0 (zero). If it would, then the flow should stop. If it will still be > 0, then the next block of code would run, which updates the tblPcardGL table.
Any assistance would be appriciated!
Thanks
declare #glID int
create table #tempGLsum
(glID int, sumAmt decimal(18,2))
insert into #tempGLsum
(glID, sumAmt)
select tblPcardReclass.glID,
sum(tblPcardReclass.reclassAmt)
from tblPcardReclass
where tblPcardReclass.glID = #glID
group by tblPcardReclass.glID
select
case when (tblPcardGL.orgAmt - tblPcardGL.amt - #tempGLsum.sumAmt) < 0
then 'stop here and let the user know it's below zero'
else
'run the code_below'
end
from tblPcardGL
left outer join #tempGLsum ON
tblPcardGL.glID = #tempGLsum.glID
where tblPcardGL.glID = #glID
-- code_below
update tblPcardGL
set amt =
(
select
case (select COUNT(*) as numRecs from #tempGLsum)
when 0 then
tblPcardGL.orgAmt
else
(tblPcardGL.orgAmt - #tempGLsum.sumAmt)
end
)
from tblPcardGL
left outer join #tempGLsum ON
tblPcardGL.glID = #tempGLsum.glID
where tblPcardGL.glID = #glID
You can't really do what you want with a case statement. You just have to do it in two steps:
First run the select with a where clause to show the user all the problems:
select tblPcardGL.*, 'Below Zero!' Error
from tblPcardGL
left outer join #tempGLsum ON
tblPcardGL.glID = #tempGLsum.glID
where tblPcardGL.glID = #glID
and (tblPcardGL.orgAmt - tblPcardGL.amt - #tempGLsum.sumAmt) < 0
Then do the update with another where clause:
update tblPcardGL
set amt =
(
select
case (select COUNT(*) as numRecs from #tempGLsum)
when 0 then
tblPcardGL.orgAmt
else
(tblPcardGL.orgAmt - #tempGLsum.sumAmt)
end
)
from tblPcardGL
left outer join #tempGLsum ON
tblPcardGL.glID = #tempGLsum.glID
where tblPcardGL.glID = #glID
and (tblPcardGL.orgAmt - tblPcardGL.amt - #tempGLsum.sumAmt) >= 0
You should consider wrapping all your #tempGLsum.sumAmt fields in isnull(#tempGLsum.sumAmt,0) because you used a left join so that column might evaluate to null, which will make any expression it is in become null.
I am a self learner in SQL; I have created this code:
SELECT T0.[CardName], T0.[DocDate], T0.[DocDueDate], T2.[U_ExpDelDate], T0.[DocStatus], T1.[SlpName],
CASE
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) <= 0 THEN 'Delivered'
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) >= 0 THEN 'Please Check'
ELSE NULL END AS 'Status',
DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) AS 'Age'
FROM OPOR T0 INNER JOIN OSLP T1 ON T0.[SlpCode] = T1.[SlpCode] INNER JOIN POR1 T2 ON T0.[DocEntry] = T2.[DocEntry]
WHERE T0.[DocStatus] ='O' and T2.[U_ExpDelDate] is not null
I am getting the right result, but now I wanted Join the result Delivered and Please Check in Age column.
Do you have any idea?
Concatenating strings in sql server is done by either using the + operator or the CONCAT method.
Try this:
SELECT T0.[CardName], T0.[DocDate], T0.[DocDueDate], T2.[U_ExpDelDate], T0.[DocStatus], T1.[SlpName],
CASE
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) <= 0 THEN 'Delivered '
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) > 0 THEN 'Please Check ' + CAST(DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) as varchar)
END AS 'Age'
FROM OPOR T0 INNER JOIN OSLP T1 ON T0.[SlpCode] = T1.[SlpCode] INNER JOIN POR1 T2 ON T0.[DocEntry] = T2.[DocEntry]
WHERE T0.[DocStatus] ='O' and T2.[U_ExpDelDate] is not null
I've removed the else part since it's never going there, the datediff function will either return a value that is less than or equal to 0 or more than 0.