SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Shift week to Wed - Tues


Shift week to Wed - Tues

Author
Message
lshanahan
lshanahan
SSChasing Mays
SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)SSChasing Mays (628 reputation)

Group: General Forum Members
Points: 628 Visits: 436
paul.j.kemna (9/27/2013)
Isn't SET DATEFIRST a sever wide setting?


Nope. Only affects the session. I've been developing code to create and update a master calendar table for our organization so I've been hip-deep in dates this past week.

____________
Just my $0.02 from over here in the cheap seats of the peanut gallery - please adjust for inflation and/or your local currency.
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5796 Visits: 11771
Jeff Moden (9/26/2013)
Michael Valentine Jones (9/26/2013)
Jeff Moden (9/26/2013)
Michael Valentine Jones (9/25/2013)
That's not really the best way to go because it depends on the setting of a run-time session parameter, DATEFIRST.


Although I certainly prefer you method, I have to ask... why does everyone have such a problem with that? It's no different than setting a variable that would be used as a constant. The only time that such a setting change would be a problem is if it were in a function, which can't be done TTBOMK.


I wanted the OP to be aware of the limitations of their method. They are certainly free to ignore my wonderful advice. Cool

I prefer to have code that works as expected under the widest possible conditions, especially if it's no harder to code.

People may be operating with different default languages than our "normal" US English, and that can change the setting of DATEFIRST, and the client application can also define that setting in an unexpected way.


Absolutely agreed across all points especially the part about it not being any more difficult. I was actually talking about putting SET DATEFIRST into the actual code, though. If someone can't figure out the integer math, I can't see why you couldn't use SET DATEFIRST for the session. People seem almost phobic about its use.


If you set DATEFIRST to something other than the expected value, there could be downstream issues in the next procedure or code that gets executed if it depends on the setting of DATEFIRST. Or if your code depends on the setting of DATEFIRST, you don't specifically set DATEFIRST, and upstream code has modified DATEFIRST to an unexpected value.

If you are going to use DATEFIRST, it is probably best to set it specifically and then reset it to the default if you are setting it to a non-default value.

If you are using connection pooling, I believe that the value of DATEFIRST does get reset to the default by the API stored procedure sp_reset_connection (unlike the isolation level Angry )


set nocount on
set datefirst 7 -- Normal US English setting
select DW1 = datepart(dw,'20130927')
go
set datefirst 3 -- Set to Wednesday
select DW2 = datepart(dw,'20130927')
go
exec ('select DW3 = datepart(dw,''20130927'')')


Results:
DW1
-----------
6

DW2
-----------
3

DW3
-----------
3

ScottPletcher
ScottPletcher
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7959 Visits: 7160
Why mess with the DATEFIRST setting when it's not necessary? There are methods which can do the calculation simply without needing a specific datefirst setting, so why hassle with it and take chances?

SQL DBA,SQL Server MVP(07, 08, 09)[size=2]Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.[/size]
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87026 Visits: 41112
ScottPletcher (9/27/2013)
Why mess with the DATEFIRST setting when it's not necessary? There are methods which can do the calculation simply without needing a specific datefirst setting, so why hassle with it and take chances?


Because, as strange as it sounds, some people just can't do the math on those simple methods.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87026 Visits: 41112
Michael Valentine Jones (9/27/2013)
Jeff Moden (9/26/2013)
Michael Valentine Jones (9/26/2013)
Jeff Moden (9/26/2013)
Michael Valentine Jones (9/25/2013)
That's not really the best way to go because it depends on the setting of a run-time session parameter, DATEFIRST.


Although I certainly prefer you method, I have to ask... why does everyone have such a problem with that? It's no different than setting a variable that would be used as a constant. The only time that such a setting change would be a problem is if it were in a function, which can't be done TTBOMK.


I wanted the OP to be aware of the limitations of their method. They are certainly free to ignore my wonderful advice. Cool

I prefer to have code that works as expected under the widest possible conditions, especially if it's no harder to code.

People may be operating with different default languages than our "normal" US English, and that can change the setting of DATEFIRST, and the client application can also define that setting in an unexpected way.


Absolutely agreed across all points especially the part about it not being any more difficult. I was actually talking about putting SET DATEFIRST into the actual code, though. If someone can't figure out the integer math, I can't see why you couldn't use SET DATEFIRST for the session. People seem almost phobic about its use.


If you set DATEFIRST to something other than the expected value, there could be downstream issues in the next procedure or code that gets executed if it depends on the setting of DATEFIRST. Or if your code depends on the setting of DATEFIRST, you don't specifically set DATEFIRST, and upstream code has modified DATEFIRST to an unexpected value.

If you are going to use DATEFIRST, it is probably best to set it specifically and then reset it to the default if you are setting it to a non-default value.

If you are using connection pooling, I believe that the value of DATEFIRST does get reset to the default by the API stored procedure sp_reset_connection (unlike the isolation level Angry )


set nocount on
set datefirst 7 -- Normal US English setting
select DW1 = datepart(dw,'20130927')
go
set datefirst 3 -- Set to Wednesday
select DW2 = datepart(dw,'20130927')
go
exec ('select DW3 = datepart(dw,''20130927'')')


Results:
DW1
-----------
6

DW2
-----------
3

DW3
-----------
3




Thanks, Michael. I keep forgetting about connection pooling. That's a REALLY good reason to not use it.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search