When selecting the data, I want the document date (belgetarihi) format to be just like the due date (vadetarihi). How to convert? Please help
SELECT
ICE_ODEMEBASLIK.*,
ORT_DOSYALAR_kayitdamga AS DSY_kayitdamga,
HP.ID HP_ID,
HP.planadi HP_planadi,
KP.ID KP_ID,
KP.planadi KP_planadi,
ICE_CARIBANKA.ID CBANKA_ID,
ICE_CARIBANKA.kod CBANKA_kod,
ICE_CARIBANKA.hesapnr CBANKA_hesapnr,
ICE_CARIBANKA.subenr CBANKA_subenr,
ICE_CARIBANKA.subeadi CBANKA_subeadi
FROM
ICE_ODEMEBASLIK WITH (NOLOCK)
LEFT JOIN
ORT_PLAN AS HP WITH (NOLOCK) ON ICE_ODEMEBASLIK.plankod = HP.plankod
LEFT JOIN
ORT_PLAN AS KP WITH (NOLOCK) ON ICE_ODEMEBASLIK.karsiplankod = KP.plankod
LEFT JOIN
(SELECT DISTINCT
ORT_DOSYALAR.kayitdamga AS ORT_DOSYALAR_kayitdamga
FROM
ORT_DOSYALAR WITH (NOLOCK)
WHERE
ORT_DOSYALAR.tablo = 6) AS DOSYA ON ICE_ODEMEBASLIK.kayitdamga = ORT_DOSYALAR_kayitdamga
LEFT JOIN
ICE_CARIBANKA WITH (NOLOCK) ON ICE_CARIBANKA.kayitdamga = ICE_ODEMEBASLIK.UQ_caribanka
WHERE
1 = 1
AND ICE_ODEMEBASLIK.IOdurum = -1
AND ICE_ODEMEBASLIK.odemetipi = 1
AND ICE_ODEMEBASLIK.pin = 0
AND ICE_ODEMEBASLIK.belgetarihi >= CONVERT(DATETIME, '01.01.2022', 103)
AND ICE_ODEMEBASLIK.belgetarihi < CONVERT(DATETIME, '01.01.2023', 103)
ORDER BY
CONVERT(DATE, ICE_ODEMEBASLIK.belgetarihi, 103),
ICE_ODEMEBASLIK.IDfisbaslik,ICE_ODEMEBASLIK.vadetarihi
here image
Related
I'm trying to update the supplier lead time in a table by calculating the difference between date of order and stock receipt date....
UPDATE cr_accs
SET cr_accs.leadtime = Avg(Datediff(day, purchord_hdr.orderdate,
stock_trans.transdate))
FROM stock_items
INNER JOIN stock_trans
ON stock_trans.stockcode = stock_items.stockcode
INNER JOIN purchord_hdr
ON purchord_hdr.seqno = stock_trans.ref1
WHERE cr_accs.accno = purchord_hdr.accno
AND stock_trans.location = 1
AND stock_trans.ref2 = 'RECEIPT'
AND purchord_hdr.orderdate >= Dateadd(day, Datediff(day, 0, Getdate()),-730)
AND stock_items.isactive = 'Y'
AND stock_items.bincode NOT IN ( 'OIO', 'CON' )
However I'm getting an error
An aggregate may not appear in the set list of an UPDATE statement
I've seen other solutions where you would change the query to:
UPDATE cr_accs
SET cr_accs.leadtime = h.calc_lead_time
FROM (SELECT AVG(DATEDIFF(day, purchord_hdr.orderdate, stock_trans.transdate)) AS calc_lead_time
FROM stock_items
INNER JOIN stock_trans
ON stock_trans.stockcode = stock_items.stockcode
INNER JOIN purchord_hdr
ON purchord_hdr.seqno = stock_trans.ref1
INNER JOIN cr_accs
ON cr_accs.accno = purchord_hdr.accno
WHERE cr_accs.accno = purchord_hdr.accno
AND stock_trans.location = 1
AND stock_trans.ref2 = 'RECEIPT'
AND purchord_hdr.orderdate >= Dateadd(day, Datediff(day, 0, Getdate()),-730)
AND stock_items.isactive = 'Y'
AND stock_items.bincode NOT IN ( 'OIO', 'CON' ) ) h
However this is not the solution for me as it doesn't define that the lead time is unique to each supplier...
It may be helpful to point out that each supplier is identified by cr_accs.accno
Any ideas please?
You can also do what you want using a correlated subquery, which might be what you were trying to do:
UPDATE cr_accs
SET cr_accs.leadtime =
(SELECT AVG(DATEDIFF(day, p.orderdate, st.transdate)) AS calc_lead_time
FROM stock_items si JOIN
stock_trans st
ON st.stockcode = si.stockcode JOIN
purchord_hdr p
ON p.seqno = st.ref1
WHERE cr_accs.accno = p.accno AND
st.location = 1 AND
st.ref2 = 'RECEIPT' AND
p.orderdate >= Dateadd(day, Datediff(day, 0, Getdate()), -730) AND
si.isactive = 'Y' AND
si.bincode NOT IN ( 'OIO', 'CON' )
);
I introduced table aliases so the query is easier to write and to read.
SQL Server also gives you the ability to express this using APPLY:
UPDATE a
SET a.leadtime = p.calc_lead_time
FROM cr_accs a CROSS APPLY
(SELECT AVG(DATEDIFF(day, p.orderdate, st.transdate)) AS calc_lead_time
FROM stock_items si JOIN
stock_trans st
ON st.stockcode = si.stockcode JOIN
purchord_hdr p
ON p.seqno = st.ref1
WHERE a.accno = p.accno AND
st.location = 1 AND
st.ref2 = 'RECEIPT' AND
p.orderdate >= Dateadd(day, Datediff(day, 0, Getdate()), -730) AND
si.isactive = 'Y' AND
si.bincode NOT IN ( 'OIO', 'CON' )
) p;
Try this join of the table to the query:
UPDATE c
SET c.leadtime = h.calc_lead_time
FROM cr_accs c
INNER JOIN (
SELECT purchord_hdr.accno,
AVG(DATEDIFF(day, purchord_hdr.orderdate, stock_trans.transdate)) AS calc_lead_time
FROM stock_items
INNER JOIN stock_trans ON stock_trans.stockcode = stock_items.stockcode
INNER JOIN purchord_hdr ON purchord_hdr.seqno = stock_trans.ref1
WHERE stock_trans.location = 1
AND stock_trans.ref2 = 'RECEIPT'
AND purchord_hdr.orderdate >= Dateadd(day, Datediff(day, 0, Getdate()),-730)
AND stock_items.isactive = 'Y'
AND stock_items.bincode NOT IN ('OIO', 'CON')
GROUP BY purchord_hdr.accno
) h ON h.accno = c.accno
I assume (by your code and the error message) that you are using SQL Server.
I'm trying to make this query work but all the time it gives:
Conversion failed when converting date and/or time from character string.
I've trying make a cast like it show in the code I do not know what else to do
In line 20 there's another idea I had but didn't work either.
SELECT 'Programadas', COUNT(*)
FROM [Base100].[dbo].[HIST_MOVIMIENTO_ENCA] enca
INNER JOIN EHC_FORMATO FORMATO ON FORMATO.CodFormato=ENCA.CodFormato
INNER JOIN [a MEDICO] as medic ON enca.Medico = medic.[nĂºmero de registro]
INNER JOIN [ESPECIALIDAD] as espc ON medic.especialidad = espc.codigo
INNER JOIN EHC_EVENTODEATENCION eve on enca.NoEvento=eve.noevento
INNER JOIN [a EMPRESA/EPS] eps on eps.nit = eve.empresa
INNER JOIN H_PACIENTE_HOSPITALIZADO HOSP on HOSP.CodigoHospitalizacion=eve.nohospitalizacion
INNER JOIN [a PACIENTE] pac ON HOSP.SecuenciaIngreso = pac.[Secuencia Ingreso]
INNER JOIN H_CAMA C ON HOSP.Habitacion_Cama = C.Habitacion_Cama
INNER JOIN H_HABITACION HAB ON C.Codigo_Hab = HAB.Codigo_Hab
INNER JOIN H_CATEGORIA_HAB CHAB ON HAB.Categoria_Hab = CHAB.Codigo_Categoria
INNER JOIN EHC_ORDENESREALIZADAS ord ON enca.nofolio = ord.nofolio
INNER JOIN EHC_MOVIMIENTODETA MD on MD.nofolio = enca.NoFolio
LEFT JOIN [EHC_ORDENESREALIZADAS] ehcord ON enca.NoFolio = ehcord.nofolio
WHERE FORMATO.CodFormato = 'SPC'
--AND (SELECT CAST(CONVERT(date, CONVERT (varchar(10), MD.respuesta), 103) as date) FROM EHC_MOVIMIENTODETA MD WHERE MD.nofolio = enca.NoFolio AND MD.CodPregunta = 'SPCA2P1')
AND CONVERT(date, SUBSTRING(MD.respuesta, 1,10)) >= CAST(DATEADD(DAY, -1, GETDATE()) as date)
AND MD.CodPregunta = 'SPCA2P1'
AND ord.estado ='P'
AND ehcord.nofolio is null
What I expect it's just the count of the result I know there's no "GROUP BY" but it's ok I just want to know that could be the problem
Use TRY_COVERT it would return NULL without failing the query, so that you can do further investigation on those null returned rows:
Example:
select
TRY_CONVERT (Date, '20190821'), --Success
TRY_CONVERT (Date, '2019 Aug 21'), --Success
TRY_CONVERT (Date, '2019 -Aug- 21'), --Success
TRY_CONVERT (Date, '2019Aug/-21') -- Failed
I've this query working fine. I need to have the sum of the column "piedini". Can you help me to undestand how to do? I obtain X row for each "Lunghezza" (lenght) that the query find. I'm not interested to this field but only to the sum o column "piedini" created from the CASE SUM.
SELECT
EXTRAMAG.PRS_LUNGHEZZA,SUM(RIGHEDOCUMENTI.QTAGEST) AS TAVOLI, CASE WHEN EXTRAMAG.PRS_LUNGHEZZA < '2000' THEN SUM(RIGHEDOCUMENTI.QTAGEST)*4 ELSE SUM(RIGHEDOCUMENTI.QTAGEST)*6 END AS PIEDINI
FROM dbo.TESTEDOCUMENTI
INNER JOIN dbo.ANAGRAFICACF
ON CODCLIFOR=CODCONTO
INNER JOIN dbo.RIGHEDOCUMENTI
ON PROGRESSIVO=IDTESTA AND TOTNETTORIGA <>'0' AND RIGHEDOCUMENTI.DESCRIZIONEART LIKE '%TAVOL%'
INNER JOIN dbo.EXTRAMAG
ON RIGHEDOCUMENTI.CODART=EXTRAMAG.CODART
LEFT JOIN .dbo.ANAGRAFICAAGENTI
ON CODAGENTE=CODAGENTE1
LEFT JOIN dbo.TABPAGAMENTI
ON CODPAGAMENTO = CODICE
WHERE dbo.TESTEDOCUMENTI.DOCCHIUSO = '0' AND dbo.TESTEDOCUMENTI.BLOCCATO = '0' AND dbo.TESTEDOCUMENTI.TIPODOC = 'ORC' AND TESTEDOCUMENTI.DATACONSEGNA BETWEEN DATEADD(DAY, -60, GETDATE()) AND GETDATE()
GROUP BY EXTRAMAG.PRS_LUNGHEZZA
Try the below :
select sum(PIEDINI) from (
SELECT XTRAMAG.PRS_LUNGHEZZA,SUM(RIGHEDOCUMENTI.QTAGEST) AS TAVOLI, CASE WHEN EXTRAMAG.PRS_LUNGHEZZA < '2000' THEN SUM(RIGHEDOCUMENTI.QTAGEST)*4 ELSE SUM(RIGHEDOCUMENTI.QTAGEST)*6 END AS PIEDINI
FROM dbo.TESTEDOCUMENTI
INNER JOIN dbo.ANAGRAFICACF
ON CODCLIFOR=CODCONTO
INNER JOIN dbo.RIGHEDOCUMENTI
ON PROGRESSIVO=IDTESTA AND TOTNETTORIGA <>'0' AND RIGHEDOCUMENTI.DESCRIZIONEART LIKE '%TAVOL%'
INNER JOIN dbo.EXTRAMAG
ON RIGHEDOCUMENTI.CODART=EXTRAMAG.CODART
LEFT JOIN .dbo.ANAGRAFICAAGENTI
ON CODAGENTE=CODAGENTE1
LEFT JOIN dbo.TABPAGAMENTI
ON CODPAGAMENTO = CODICE
WHERE dbo.TESTEDOCUMENTI.DOCCHIUSO = '0' AND dbo.TESTEDOCUMENTI.BLOCCATO = '0' AND dbo.TESTEDOCUMENTI.TIPODOC = 'ORC' AND TESTEDOCUMENTI.DATACONSEGNA BETWEEN DATEADD(DAY, -60, GETDATE()) AND GETDATE()
GROUP BY EXTRAMAG.PRS_LUNGHEZZA
)t
I have this stored procedure...
SELECT
LTRIM(RTRIM([OF].OF_DISPLAYNAME)) AS [Officer_Name],
LTRIM(RTRIM([OF].OF_PIN)) AS Officer_Pin,
LTRIM(RTRIM([TO].TO_ACTIVITY)) AS Template_Site,
AC.AC_NAME AS Site_Name,
ZN.ZN_MANAGER AS Site_Manager,
AR.AR_DESCRIPTION AS Scheduler,
CONVERT(VARCHAR(10), #WeekCommencing, 6) AS [Week_Commencing],
CONVERT(VARCHAR(10), [OF].OF_DOL, 6) AS Leave_Date,
CAST((SELECT
(ISNULL(SUM(RO.RO_SHIFT_LENGTH - RO_BREAK_LENGTH), 0.0)/60.0)
FROM
RoleCall.dbo.ROSTER AS RO WITH(NOLOCK)
WHERE
RO.RO_OFFICER = [OF].OF_PIN
AND RO.RO_SHIFT_START BETWEEN #WeekCommencing AND #WeekEnding
AND RO.RO_STATUS IN ('COMP', 'CONF', 'TODO')
) AS DECIMAL(11,2)) AS HoursSum,
RSWH.dbo.RSWHF_GetMarker([OF].OF_PIN, #WeekCommencing, #WeekEnding) AS Marker,
CONVERT(VARCHAR(10), (SELECT MAX(RO.RO_SHIFT_START)
FROM RoleCall.dbo.ROSTER AS RO WITH(NOLOCK)
WHERE RO.RO_OFFICER = [OF].OF_PIN
AND RO.RO_SHIFT_START < GETDATE()
AND RO.RO_STATUS IN ('COMP', 'CONF', 'TODO')
), 6) AS Last_Worked_Shift,
CONVERT(VARCHAR(10), (SELECT TOP 1
HC.Effective_Date
FROM HR.dbo.HRST_Contract AS HC WITH(NOLOCK)
WHERE
HC.Of_Pin = [OF].OF_PIN
AND HC.SD_Contract = 1
ORDER BY
Effective_Date DESC
), 6) AS Contract_Sent_Date,
CONVERT(VARCHAR(10),
(
SELECT TOP 1
RO.RO_SHIFT_START
FROM
Rolecall.dbo.Roster AS RO WITH(NOLOCK)
WHERE
RO.RO_Activity = 'ENDASSIG'
AND RO.RO_STATUS <> 'CANC'
AND RO.RO_OFFICER =[OF].OF_PIN
AND RO.RO_SHIFT_START < #WeekEnding
ORDER BY
RO.RO_SHIFT_START DESC
), 6) AS Ass_End_Date,
ISNULL(CreatedBy, '') AS Checked_By,
---ISSUE IS OCCURRING HERE----
(
SELECT
SUM(a.[Value*NumofActivity]) + max(b.NumOfHoursWorkedInTheLast12Weeks) AS [TotalHoursWorkedInTheLast12Weeks],Markers
FROM
(
SELECT
RO_OFFICER [RO_OFFICER],RO_ACTIVITY , COUNT(RO_ACTIVITY)*Value as [Value*NumofActivity]
FROM
RoleCall.dbo.ROSTER as ro inner join [RSWH].DBO.[RSWHT_Hours_Assigned_To_Markers_LookUp] AS MR on RO_ACTIVITY = MR.MARKER
WHERE
ro_officer = [OF].OF_PIN
AND RO_STATUS = 'INFO'
AND RO_SHIFT_START between DATEADD(week, -12, GETDATE()) AND GETDATE()
GROUP BY
RO_OFFICER,RO_ACTIVITY,Value) a
JOIN
(
SELECT
RO_OFFICER [RO_OFFICER] ,(ISNULL(SUM(RO_SHIFT_LENGTH - RO_BREAK_LENGTH), 0.0)/60.0)as [NumOfHoursWorkedInTheLast12Weeks],RSWH.dbo.RSWHF_GetMarker(RO_OFFICER, DATEADD(week, -12, GETDATE()), GETDATE()) AS Markers
FROM
ROLECALL.DBO.ROSTER as ro inner join [RSWH].DBO.[RSWHT_Hours_Assigned_To_Markers_LookUp] AS MR on RO_ACTIVITY = MR.MARKER
WHERE
RO_SHIFT_START >= DATEADD(week, -12, GETDATE()) AND RO_SHIFT_START <= GETDATE()
AND RO.RO_STATUS = 'INFO'
AND RO_OFFICER = [OF].OF_PIN
GROUP BY
RO_OFFICER
) b on a.RO_OFFICER = b.RO_OFFICER
GROUP BY A.RO_OFFICER,b.NumOfHoursWorkedInTheLast12Weeks,Markers)
FROM
RoleCall.dbo.OFFICER AS [OF] WITH (NOLOCK)
LEFT JOIN RoleCall.dbo.TEMPLATE AS [TO] WITH (NOLOCK) ON [TO].TO_PIN = [OF].OF_PIN AND [TO].TO_TYPE COLLATE Latin1_General_CS_AS = 'M'
LEFT JOIN RoleCall.dbo.ACTIVITY AS AC WITH (NOLOCK) ON AC.AC_PIN = [TO].TO_ACTIVITY
LEFT JOIN RoleCall.dbo.ZONE AS ZN WITH (NOLOCK) ON ZN.ZN_ZONE = AC.AC_ZONE
LEFT JOIN RoleCall.dbo.AREA AS AR WITH (NOLOCK) ON AR.AR_AREA = AC.AC_AREA
LEFT JOIN Appollo.ACMS.dbo.ACSMT_Checked_Officer AS CO WITH (NOLOCK) ON CO.OfficerPin = [OF].OF_PIN AND WeekCommencing BETWEEN #WeekCommencing AND #WeekEnding
WHERE
[OF].OF_PAYROLL_NO IN ('W', 'S')
AND [OF].OF_SKILLS = #Of_Skills --
AND (#Of_Skills = 'R' OR AC.AC_PARENT IN ('9947', '9133'))
AND ([OF].OF_DOL IS NULL OR [OF].OF_DOL > #WeekCommencing)
AND [OF].OF_RANK <> 'PT'
AND [OF].OF_RANK <> 'LW'
AND EXISTS (
SELECT
TOP 1 Of_Pin
FROM
HR.dbo.HRST_Contract AS HC WITH(NOLOCK)
WHERE
Of_Pin = [OF].OF_PIN
AND ISNULL(SD_Contract, 0) = 1
)
AND (
SELECT
ISNULL(SUM(RO.RO_SHIFT_LENGTH - RO_BREAK_LENGTH), 0.0)
FROM
RoleCall.dbo.ROSTER AS RO WITH(NOLOCK)
WHERE
RO.RO_OFFICER = [OF].OF_PIN
AND RO.RO_SHIFT_START BETWEEN #WeekCommencing AND #WeekEnding
AND (RO.RO_STATUS IN ('COMP', 'CONF', 'TODO') OR (RO_Activity ='LEAVE' AND RO_STATUS <> 'CANC'))
) < 420.0
AND (ISNULL(CreatedBy, '') = '' OR #UncheckedOfficersOnly = 0)
ORDER BY
Officer_Name,
Officer_Pin
The issue is I need to return the three columns which I'm trying to sum. I haven't got much experience in SQL and I was wondering if anyone has any suggestions or pointers for this to be done.
Thank you
The code below (and numerous codes like it) continues to return blanks.
Basically, if LaborCode = '01 - SC' then it should sort by JobSite, LaborCode, and Schedule (the exact date)
If it's NOT 01 - SC, it should sort by JobSite, LaborCode, and DayNo (the day of the week)
Select Distinct Agreements.AgrmntID, Agreements.Description, Agreements.Status,
JobSites.SiteName, JobSites.Address2, JobSites.City, Customers.CustName,
Customers.CompanyName, LaborCodeTypes.RepairCode As LaborCode, Schedule = Case
LaborCodeTypes.RepairCode
When '01 - SC' Then Left(Convert(varchar,AgreementSchedules.SchedDate,110),
10) Else DateName(dw, AgreementSchedules.SchedDate)
End, Employees1.EmpName As Vendor, Employees.EmpName As [Area Manager],
DatePart(dw, AgreementSchedules.SchedDate) As DayNo
From Agreements Inner Join
Customers On Agreements.CustID = Customers.CustID Inner Join
AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID
Inner Join
JobSites On Agreements.CustSiteID = JobSites.CustSiteID Left Outer Join
LaborCodeTypes On AgreementSchedules.RepairID = LaborCodeTypes.RepairID
Left Outer Join
Employees On AgreementSchedules.FormanEmpID = Employees.EmployeeID Left Join
WorkOrderSchedules On WorkOrderSchedules.ScheduleID =
AgreementSchedules.ScheduleID And AgreementSchedules.ScheduleID =
WorkOrderSchedules.ScheduleID Left Join
WorkOrderScheduleTechs On WorkOrderSchedules.ScheduleID =
WorkOrderScheduleTechs.ScheduleID Left Join
Employees Employees1 On WorkOrderScheduleTechs.EmployeeID =
Employees1.EmployeeID
Where Agreements.Status = 2 And LaborCodeTypes.RepairCode <> 'Vendor Bill' And
Month(AgreementSchedules.SchedDate) = Month(GetDate())
Order By Case
When [LaborCodeTypes.RepairCode] In ('01 - SC') Then JobSites.SiteName +
LaborCodeTypes.RepairCode + Schedule
Else JobSites.SiteName + LaborCodeTypes.RepairCode + DayNo End
Thank you for your help!!
Try this:
ORDER BY JobSites.SiteName,
LaborCodeTypes.RepairCode,
CASE WHEN LaborCodeTypes.RepairCode IN ('01 - SC') THEN Schedule ELSE DayNo END
Okay, in SQL Server:
CREATE PROCEDURE dbo.Agreements_GetList
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
a.AgrmntID,
a.Description,
a.Status,
js.SiteName,
js.Address2,
js.City,
c.CustName,
c.CompanyName,
LaborCode = lct.RepairCode,
Schedule = CASE lct.RepairCode
WHEN '01 - SC' THEN CONVERT(CHAR(10), aSch.SchedDate, 110)
--------------------^^^ much better than LEFT and not specifying length
ELSE DATENAME(WEEKDAY, aSch.SchedDate) END,
Vendor = e2.EmpName,
[Area Manager] = e1.EmpName,
DayNo = CONVERT(CHAR(1), DATEPART(WEEKDAY, aSch.SchedDate))
--------^^^ this convert is important
FROM dbo.Agreements AS a
INNER JOIN dbo.Customers AS c
ON a.CustID = c.CustID
INNER JOIN dbo.AgreementSchedules AS aSch
ON a.AgrmntID = aSch.AgrmntID
INNER JOIN dbo.JobSites AS js
ON a.CustSiteID = js.CustSiteID
LEFT OUTER JOIN dbo.LaborCodeTypes AS lct
ON aSch.RepairID = lct.RepairID
LEFT OUTER JOIN dbo.Employees AS e1
ON aSch.FormanEmpID = e1.EmployeeID
LEFT OUTER JOIN dbo.WorkOrderSchedules AS w
ON w.ScheduleID = aSch.ScheduleID
LEFT OUTER JOIN dbo.WorkOrderScheduleTechs AS wt
ON w.ScheduleID = wt.ScheduleID
LEFT OUTER JOIN dbo.Employees AS e2
ON wt.EmployeeID = e2.EmployeeID
WHERE
a.Status = 2
AND lct.RepairCode <> 'Vendor Bill'
AND aSch.SchedDate >= DATEADD(MONTH, 0, DATEDIFF(MONTH, 0, GETDATE()))
AND aSch.SchedDate < DATEADD(MONTH, 1, DATEDIFF(MONTH, 0, GETDATE()))
ORDER BY
js.SiteName,
lct.RepairCode,
CASE WHEN lct.RepairCode = '01 - SC'
THEN js.SiteName ELSE DayNo END;
END
GO
Now I have absolutely no knowledge of your application, so you will have to figure out how to call a stored procedure from it instead of embedding SQL.