SQL Server dateformat questionable behavior
up vote
-3
down vote
favorite
Put this piece of code in query analyzer and hit F5.
declare @aaa datetime
set @aaa='01/12/2011'
set dateformat dmy
select month(@aaa)
It gives you 1. Hit F5 again, it gives you 12. Why?
sql-server
add a comment |
up vote
-3
down vote
favorite
Put this piece of code in query analyzer and hit F5.
declare @aaa datetime
set @aaa='01/12/2011'
set dateformat dmy
select month(@aaa)
It gives you 1. Hit F5 again, it gives you 12. Why?
sql-server
4
Query Analyzer? Have I been time-warped back 10 years?
– Aaron Bertrand
Feb 21 '12 at 15:03
1
If you use a language-/location-independent string-based date (e.g. the ISO-8601 standard formatYYYYMMDD
) - e.g.set @aaa = '20111201'
- then you can switch around between date formats as many times as you like - you'll ALWAYS get12
for the month ......
– marc_s
Feb 21 '12 at 15:04
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Put this piece of code in query analyzer and hit F5.
declare @aaa datetime
set @aaa='01/12/2011'
set dateformat dmy
select month(@aaa)
It gives you 1. Hit F5 again, it gives you 12. Why?
sql-server
Put this piece of code in query analyzer and hit F5.
declare @aaa datetime
set @aaa='01/12/2011'
set dateformat dmy
select month(@aaa)
It gives you 1. Hit F5 again, it gives you 12. Why?
sql-server
sql-server
edited Feb 21 '12 at 15:09
Aaron Bertrand
206k27360401
206k27360401
asked Feb 21 '12 at 15:00
Florin
11
11
4
Query Analyzer? Have I been time-warped back 10 years?
– Aaron Bertrand
Feb 21 '12 at 15:03
1
If you use a language-/location-independent string-based date (e.g. the ISO-8601 standard formatYYYYMMDD
) - e.g.set @aaa = '20111201'
- then you can switch around between date formats as many times as you like - you'll ALWAYS get12
for the month ......
– marc_s
Feb 21 '12 at 15:04
add a comment |
4
Query Analyzer? Have I been time-warped back 10 years?
– Aaron Bertrand
Feb 21 '12 at 15:03
1
If you use a language-/location-independent string-based date (e.g. the ISO-8601 standard formatYYYYMMDD
) - e.g.set @aaa = '20111201'
- then you can switch around between date formats as many times as you like - you'll ALWAYS get12
for the month ......
– marc_s
Feb 21 '12 at 15:04
4
4
Query Analyzer? Have I been time-warped back 10 years?
– Aaron Bertrand
Feb 21 '12 at 15:03
Query Analyzer? Have I been time-warped back 10 years?
– Aaron Bertrand
Feb 21 '12 at 15:03
1
1
If you use a language-/location-independent string-based date (e.g. the ISO-8601 standard format
YYYYMMDD
) - e.g. set @aaa = '20111201'
- then you can switch around between date formats as many times as you like - you'll ALWAYS get 12
for the month ......– marc_s
Feb 21 '12 at 15:04
If you use a language-/location-independent string-based date (e.g. the ISO-8601 standard format
YYYYMMDD
) - e.g. set @aaa = '20111201'
- then you can switch around between date formats as many times as you like - you'll ALWAYS get 12
for the month ......– marc_s
Feb 21 '12 at 15:04
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
This is because your line set dateformat dmy
is half way through the script.
This is switching your date format after the initial assignment for the first 'F5' but then this is persisted for the second 'F5'
If you make it the first line it should be consistent and return 12 each time
To be clear - the set dateformat dmy
makes a difference for the set @aaa='01/12/2011'
line as it affects the way that the string '01/12/2011'
is parsed to a datetime.
On the first run, the @aaa will actually contain the date '12-Jan-2011', whereas after you set dateformat dmy
it will contain the date '01-Dec-2011'.
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
1
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
1
'dd/mm/yyyy'
is not a type.
– Aaron Bertrand
Feb 21 '12 at 15:36
|
show 4 more comments
up vote
1
down vote
Because the first time you ran the code, the date was stored internally as January 12th. The set setting sticks to your session, then you ran it again so you redeclared the variable, and this time it was interpreted as December 1st. The set setting doesn't affect datepart functions like MONTH()
because the date has already been interpreted and isn't stored internally the way you've typed it.
Instead of declaring date literals with regional and ambiguous formatting, you should always use safe, unambiguous formats. For datetime/smalldatetime:
yyyymmdd
yyyy-mm-ddThh:mm:ss
For date:
yyyy-mm-dd
If you want to display dates in these friendly but confusing formats, by all means do that at the presentation layer. But don't try to confuse SQL Server with them. Please read this blog post and this article by Tibor Karaszi.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This is because your line set dateformat dmy
is half way through the script.
This is switching your date format after the initial assignment for the first 'F5' but then this is persisted for the second 'F5'
If you make it the first line it should be consistent and return 12 each time
To be clear - the set dateformat dmy
makes a difference for the set @aaa='01/12/2011'
line as it affects the way that the string '01/12/2011'
is parsed to a datetime.
On the first run, the @aaa will actually contain the date '12-Jan-2011', whereas after you set dateformat dmy
it will contain the date '01-Dec-2011'.
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
1
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
1
'dd/mm/yyyy'
is not a type.
– Aaron Bertrand
Feb 21 '12 at 15:36
|
show 4 more comments
up vote
1
down vote
This is because your line set dateformat dmy
is half way through the script.
This is switching your date format after the initial assignment for the first 'F5' but then this is persisted for the second 'F5'
If you make it the first line it should be consistent and return 12 each time
To be clear - the set dateformat dmy
makes a difference for the set @aaa='01/12/2011'
line as it affects the way that the string '01/12/2011'
is parsed to a datetime.
On the first run, the @aaa will actually contain the date '12-Jan-2011', whereas after you set dateformat dmy
it will contain the date '01-Dec-2011'.
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
1
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
1
'dd/mm/yyyy'
is not a type.
– Aaron Bertrand
Feb 21 '12 at 15:36
|
show 4 more comments
up vote
1
down vote
up vote
1
down vote
This is because your line set dateformat dmy
is half way through the script.
This is switching your date format after the initial assignment for the first 'F5' but then this is persisted for the second 'F5'
If you make it the first line it should be consistent and return 12 each time
To be clear - the set dateformat dmy
makes a difference for the set @aaa='01/12/2011'
line as it affects the way that the string '01/12/2011'
is parsed to a datetime.
On the first run, the @aaa will actually contain the date '12-Jan-2011', whereas after you set dateformat dmy
it will contain the date '01-Dec-2011'.
This is because your line set dateformat dmy
is half way through the script.
This is switching your date format after the initial assignment for the first 'F5' but then this is persisted for the second 'F5'
If you make it the first line it should be consistent and return 12 each time
To be clear - the set dateformat dmy
makes a difference for the set @aaa='01/12/2011'
line as it affects the way that the string '01/12/2011'
is parsed to a datetime.
On the first run, the @aaa will actually contain the date '12-Jan-2011', whereas after you set dateformat dmy
it will contain the date '01-Dec-2011'.
answered Feb 21 '12 at 15:03
Jon Egerton
29.4k1070112
29.4k1070112
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
1
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
1
'dd/mm/yyyy'
is not a type.
– Aaron Bertrand
Feb 21 '12 at 15:36
|
show 4 more comments
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
1
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
1
'dd/mm/yyyy'
is not a type.
– Aaron Bertrand
Feb 21 '12 at 15:36
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
set dateformat dmy it has to be there. @aaa is just a parmeter received by the stored procedure. The first 2 lines is to try the procedure at design time
– Florin
Feb 21 '12 at 15:09
1
1
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
@Florin: That wasn't stated in the question - the more information you give, the better an answer you get. Where is the stored proc called from?
– Jon Egerton
Feb 21 '12 at 15:20
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
OK. Sorry. Let's see. You have this procedure: CREATE PROCEDURE [dbo].[Test] @dDate datetime AS BEGIN SET NOCOUNT ON; set dateformat dmy select month(@dDate) END If you execute the procedure it return 1 instead of 12
– Florin
Feb 21 '12 at 15:24
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
@Florin: That still doesn't state where the proc is called from. Are you calling from sql code, or from elsewhere (eg .net)
– Jon Egerton
Feb 21 '12 at 15:26
1
1
'dd/mm/yyyy'
is not a type.– Aaron Bertrand
Feb 21 '12 at 15:36
'dd/mm/yyyy'
is not a type.– Aaron Bertrand
Feb 21 '12 at 15:36
|
show 4 more comments
up vote
1
down vote
Because the first time you ran the code, the date was stored internally as January 12th. The set setting sticks to your session, then you ran it again so you redeclared the variable, and this time it was interpreted as December 1st. The set setting doesn't affect datepart functions like MONTH()
because the date has already been interpreted and isn't stored internally the way you've typed it.
Instead of declaring date literals with regional and ambiguous formatting, you should always use safe, unambiguous formats. For datetime/smalldatetime:
yyyymmdd
yyyy-mm-ddThh:mm:ss
For date:
yyyy-mm-dd
If you want to display dates in these friendly but confusing formats, by all means do that at the presentation layer. But don't try to confuse SQL Server with them. Please read this blog post and this article by Tibor Karaszi.
add a comment |
up vote
1
down vote
Because the first time you ran the code, the date was stored internally as January 12th. The set setting sticks to your session, then you ran it again so you redeclared the variable, and this time it was interpreted as December 1st. The set setting doesn't affect datepart functions like MONTH()
because the date has already been interpreted and isn't stored internally the way you've typed it.
Instead of declaring date literals with regional and ambiguous formatting, you should always use safe, unambiguous formats. For datetime/smalldatetime:
yyyymmdd
yyyy-mm-ddThh:mm:ss
For date:
yyyy-mm-dd
If you want to display dates in these friendly but confusing formats, by all means do that at the presentation layer. But don't try to confuse SQL Server with them. Please read this blog post and this article by Tibor Karaszi.
add a comment |
up vote
1
down vote
up vote
1
down vote
Because the first time you ran the code, the date was stored internally as January 12th. The set setting sticks to your session, then you ran it again so you redeclared the variable, and this time it was interpreted as December 1st. The set setting doesn't affect datepart functions like MONTH()
because the date has already been interpreted and isn't stored internally the way you've typed it.
Instead of declaring date literals with regional and ambiguous formatting, you should always use safe, unambiguous formats. For datetime/smalldatetime:
yyyymmdd
yyyy-mm-ddThh:mm:ss
For date:
yyyy-mm-dd
If you want to display dates in these friendly but confusing formats, by all means do that at the presentation layer. But don't try to confuse SQL Server with them. Please read this blog post and this article by Tibor Karaszi.
Because the first time you ran the code, the date was stored internally as January 12th. The set setting sticks to your session, then you ran it again so you redeclared the variable, and this time it was interpreted as December 1st. The set setting doesn't affect datepart functions like MONTH()
because the date has already been interpreted and isn't stored internally the way you've typed it.
Instead of declaring date literals with regional and ambiguous formatting, you should always use safe, unambiguous formats. For datetime/smalldatetime:
yyyymmdd
yyyy-mm-ddThh:mm:ss
For date:
yyyy-mm-dd
If you want to display dates in these friendly but confusing formats, by all means do that at the presentation layer. But don't try to confuse SQL Server with them. Please read this blog post and this article by Tibor Karaszi.
edited Nov 10 at 22:50
answered Feb 21 '12 at 15:04
Aaron Bertrand
206k27360401
206k27360401
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9379756%2fsql-server-dateformat-questionable-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Query Analyzer? Have I been time-warped back 10 years?
– Aaron Bertrand
Feb 21 '12 at 15:03
1
If you use a language-/location-independent string-based date (e.g. the ISO-8601 standard format
YYYYMMDD
) - e.g.set @aaa = '20111201'
- then you can switch around between date formats as many times as you like - you'll ALWAYS get12
for the month ......– marc_s
Feb 21 '12 at 15:04