danjonescidtrix danjonescidtrix - 2 months ago 19
SQL Question

datename conversion fails in SQL agent only

So I am executing a stored procedure through Agents in SQL Server management studio. When I run my stored procedure normally, it runs fine returning no errors, however when it runs from the Agent it fails to run.

My stored procedure:

USE [Sales]
GO
/****** Object: StoredProcedure [dbo].[forecastprojSPChartDetailsForPlaInsert] Script Date: 29/09/2016 12:50:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[forecastprojSPChartDetailsForPlaInsert]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

Select DISTINCT

MIN(S.Id) As Id
, S.DATE_DEL
,'0.00' AS Installation
, SD.ProjectNo
, SHA.[Customer Name]
, P2.[Project Name]
, AD.ApplicationDate
, AD.IssueOfPaymentNotice
, AD.FinalDateForSubPayment
, AD.ValuationDate
, CF.Comment
, CF.QsNotified
, CF.AccountsNotified
, CF.CallBackDate
, CF.PaidAmount
, CF.PrevPaidOtherTextBox
, CF.ConfirmedFinalDate
, CF.ConfirmedPaidDate
, CF.RemovedCheck
, CF.RemovedCheckComment
, 'No' as SO
, CF.ApplicationDateChanged

,datename(month,SC.DateOfSend)
,datename(month,S.Date_Del)
,datename(month,AD.ApplicationDate)

from SODDFD S
INNER JOIN SpecDetails SD ON SD.SrecId = S.Srecid
INNER JOIN SpecificationHeader SHA ON SD.QuoteNo = SHA.Qno
INNER JOIN Projects2 P2 ON P2.PID = SHA.PID
INNER JOIN SentContracts SC on SC.PID = P2.PID and SC.DD = '1'
INNER JOIN ApplicationDates AD on SHA.PID = AD.PID

AND
CASE WHEN CONVERT(DATE,GETDATE(),103) > CONVERT(DATE,AD.ApplicationDate,103) THEN datename(month,SC.DateOfSend)
ELSE datename(month,S.Date_Del)
END = datename(month,AD.ApplicationDate)

LEFT OUTER JOIN SentappDetails SA ON SA.SrecId = SD.SrecId
LEFT JOIN CashFlowProjectionTbl CF on AD.PID = CF.PID and AD.ApplicationDate = CF.ApplicationDate

where
S.PID in (Select distinct PID from ApplicationDates) AND
S.Id <> 0 AND
S.Id <> '' AND
Nominal = CASE WHEN SD.NewSellingPrice IS NULL THEN Nominal ELSE 'S001' END AND
s.DATE_DEL <> ''AND
(AD.ApplicationDate like '%/2016' or ConfirmedFinalDate <> '' or ConfirmedFinalDate IS NOT NULL)
and (convert(date,IssueOfPaymentNotice,103) >= '01/05/2016' or ConfirmedFinalDate <> '' or ConfirmedFinalDate IS NOT NULL)
--and (Cf.PaidAmount = '0' or Cf.PaidAmount = '0.00' or Cf.PaidAmount = '' or Cf.PaidAmount is NULL)
and (RemovedCheck = '' or RemovedCheck is NULL)
and convert(date,Ad.ApplicationDate,103) >= '01/06/2016'


GROUP BY S.PID, SD.NewSellingPrice, s.DATE_DEL, SA.CURR_APP, s.DEL_QTY, Comment, QsNotified, AccountsNotified,
CASE WHEN SD.NewSellingPrice IS NULL THEN S.Id END ,SD.ProjectNo, SHA.[Customer Name], P2.[Project Name],
AD.ApplicationDate, ad.IssueOfPaymentNotice, AD.FinalDateForSubPayment, CF.CallBackDate,
CF.PaidAmount , CF.PrevPaidOtherTextBox, CF.ConfirmedFinalDate, AD.ValuationDate, CF.ConfirmedPaidDate, CF.RemovedCheck, CF.RemovedCheckComment, CF.ApplicationDateChanged
,datename(month,SC.DateOfSend)
,datename(month,S.Date_Del)
,datename(month,AD.ApplicationDate)


END


When this runs from my agent it fails returning this error:

enter image description here

However if I replace:

AND
CASE WHEN CONVERT(DATE,GETDATE(),103) > CONVERT(DATE,AD.ApplicationDate,103) THEN datename(month,SC.DateOfSend)
ELSE datename(month,S.Date_Del)
END = datename(month,AD.ApplicationDate)


With:

AND
CASE WHEN CONVERT(DATE,GETDATE(),103) > CONVERT(DATE,AD.ApplicationDate,103) THEN 'July'
ELSE 'July'
END = 'July'


...it runs fine.

The only thing I can think is that there is an error in my "DATENAME" conversions when it runs from my Agent. Bearing in mind the date that I am passing in:


  • SC.DateOfSend

  • S.Date_Del

  • AD.ApplicationDate



are all dd/MM/yyyy format.

I was wondering if anyone could help me or advice me what to do as I have been stuck on this for hours. Thankyou in advance.

Answer

You need to change server default or your login default language

SELECT @@LANGUAGE 

Function DATENAME depends on language

SET LANGUAGE us_english

Try to capture at beginning and at the end set it again as is was

declare @language as varchar(100)
SELECT @language= @@LANGUAGE 

SET LANGUAGE us_english 

--... your code...

SET LANGUAGE @language