April 15, 2024 at 9:26 am
I am running SQL2012 (don't ask...) and my collation is Latin1_General_CI_AS and my language code is 1033 (British English) at both instance and database level.
I have the following code executing in an SQL Agent job
INSERT INTO DEV.scratch
(
... List of Fileds
)
EXEC [DEV].[ADR_TEST]
Part of the logic restricts records to only return data up to the end of the previous week
WHERE
RL.LedgerDate >=
(
SELECT
MIN(FW.FinWKStart) AS [start]
FROM
FinWeek AS FW
WHERE
FW.FinYear = @ReportingYear
)
/*But don't include transactions in the week the report is run (i.e. only up to last Sunday)*/
AND
RL.LedgerDate <=
(
SELECT DATEADD(DAY,-1,DATEADD(DAY,DATEPART(WEEKDAY,GETDATE()) * -1, CAST(GETDATE() AS DATE)))
)
I am in the UK so Monday = WEEKDAY = 1. The financial year in the FinWeek table starts 1st April 2024. When I run the code from Management Studio in a query window on 15th April, I get 2 weeks of data (2nd week ended 14th April). When I run the same code from SQL agent job, it only returns the first weeks data, which is an incorrect response.
I am making the assumption that the SQL agent job is calculating the WEEKDAY differently, and if that is the case, why does it not respect the instance or database settings, and how do I enforce the correct calculation. If the results are inconsistent for a different reason, I am all ears.
April 15, 2024 at 9:56 am
I would use the following code to get 'last' Sunday. Seems a bit simpler than the current version.
SELECT DATEADD (wk, DATEDIFF (wk, 0, GETDATE ()), 0) - 1;
Also, you could try explicitly forcing the issue by running SET DATEFIRST.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 15, 2024 at 10:56 am
I have resorted to setting the DATEFIRST in the SQL Agent job, but I still want to know WHY SQL agent and Query window behave differently.
April 15, 2024 at 1:22 pm
Just use a method that works under any/all DATEFIRST settings, much simpler and safer:
/* calc immediately previous Sunday; day 0 = Monday, so day 6 = Sunday */
SELECT DATEADD(DAY, -DATEDIFF(DAY, 6, GETDATE()) % 7, CAST(GETDATE() AS date))
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 15, 2024 at 1:38 pm
Do not use DATEDIFF(WK) if you need for the first of the week to be based on any day of the week other than SUNDAY. It is ALWAYS based on SUNDAY. Don't take my word for it, though... Please read the 3rd paragraph in the following section of the MS documentation on DATEDIFF...
For those that haven't had enough coffee this morning, the documentation states it, plain and simple, as follows...
Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF always uses Sunday as the first day of the week to ensure the function operates in a deterministic way.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 15, 2024 at 2:01 pm
I would use the following code to get 'last' Sunday. Seems a bit simpler than the current version.
SELECT DATEADD (wk, DATEDIFF (wk, 0, GETDATE ()), 0) - 1;Also, you could try explicitly forcing the issue by running SET DATEFIRST.
Or, forget the Week datepart. Use the tried and true method of always having it be right for Mondays (of course, change the 0 appropriately for other days of the week).
SELECT FirstMondayCurWeek = DATEADD(dd,DATEDIFF(dd,0,GETDATE())/7*7,0);
Of course, if you want your entire team to stop messing around and having 72 different versions of such code (and many are actually incorrect), please see the following article...
As a bit of a sidebar, I try to avoid the use of DATEFIRST like the plague because way too much changes if you send your code "overseas" or to have it work remotely in a different company. It's just not worth it to me. YMMV.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 15, 2024 at 2:20 pm
This was removed by the editor as SPAM
April 15, 2024 at 2:59 pm
one other thing that can cause differences is the timezone of the server and the time /day the job runs on the server.
April 15, 2024 at 3:15 pm
I have resorted to setting the DATEFIRST in the SQL Agent job, but I still want to know WHY SQL agent and Query window behave differently.
Different users can be set up with different settings. Maybe the Agent user has different settings to the user you are using in the query window?
April 15, 2024 at 3:15 pm
I have resorted to setting the DATEFIRST in the SQL Agent job, but I still want to know WHY SQL agent and Query window behave differently.
Different users can be set up with different settings. Maybe the Agent user has different settings to the user you are using in the query window?
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply