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


Weird Question About Querying with Dates


Weird Question About Querying with Dates

Author
Message
Polymorphist
Polymorphist
Old Hand
Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)

Group: General Forum Members
Points: 357 Visits: 269
Okay so this is a very odd situation, one that I have not seen before.

I'm trying to query a table which stores dates in the following format - '2013-02-13 00:00:00.000'. When I try to query the table between two dates - 2012-08-01 00:00:00.000 and 2013-02-13 00:00:00.000 it only returns a certain number of rows which doesn't look right. In other words, it's only returning one store location for the entire date range. Sure enough, when I query the entire table I get the full list of store locations returning between those two dates. I don't get it - what causes this?

The reason I ask is because I don't have a way to test my logic in DEV. The production environment was changed this weekend and i'm updating the stored procedures, however the DEV environment was not changed for whatever reason. I don't have access to production. I have to create my own test tables based on the dates. I haven't used SQL Server 2005 in a long time so finding the date issue perplexing (normally, i'd just cast to date type).

Thanks!!
Polymorphist
Polymorphist
Old Hand
Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)

Group: General Forum Members
Points: 357 Visits: 269
Sorry, I managed to figure out what was going on. It actually had nothing to do with the dates.

Ugh, i'm under a little bit of pressure this weekend which is causing me stress. I wasn't given all of the info regarding the client environment and not having a way to test my changes is driving me crazy. Unfortunately I don't have a way to contact them over the weekend. This will be the LAST TIME that I take a short assignment without speaking to the client first. Man, things are really messed-up there, LOL.
Eric M Russell
Eric M Russell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29659 Visits: 11549
SQL Server doesn't store date/times values in a format, it's just a typed integer.

However, SET DATEFORMAT setting influences how date/time values are displayed in a resultset and how strings are converted to date/time values. Also, when using the DateTimeOffset datatype, time zone offset comes into play.

When comparing a date/time value to a range, I'd reccomend doing it like this rather than doing a BETWEEN:

WHERE DateColumn >= '2012-08-01' AND DateColumn < '2013-02-14'

This insures that you're looking at only date and the time portion is ingored. Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.


"The universe is complicated and for the most part beyond your control, but your life is only as complicated as you choose it to be."
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97708 Visits: 38989
Eric M Russell (2/18/2013)
SQL Server doesn't store date/times values in a format, it's just a typed integer.

However, SET DATEFORMAT setting influences how date/time values are displayed in a resultset and how strings are converted to date/time values. Also, when using the DateTimeOffset datatype, time zone offset comes into play.

When comparing a date/time value to a range, I'd reccomend doing it like this rather than doing a BETWEEN:

WHERE DateColumn >= '2012-08-01' AND DateColumn < '2013-02-14'

This insures that you're looking at only date and the time portion is ingored. Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



Not true:



Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



The 'YYYY-MM-DD' string format is affected by the setting of DATEFORMAT. Using 'YYYYMMDD' is not affected by the setting of DATEFORMAT.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Eric M Russell
Eric M Russell
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29659 Visits: 11549
Lynn Pettis (2/18/2013)
Eric M Russell (2/18/2013)
SQL Server doesn't store date/times values in a format, it's just a typed integer.

However, SET DATEFORMAT setting influences how date/time values are displayed in a resultset and how strings are converted to date/time values. Also, when using the DateTimeOffset datatype, time zone offset comes into play.

When comparing a date/time value to a range, I'd reccomend doing it like this rather than doing a BETWEEN:

WHERE DateColumn >= '2012-08-01' AND DateColumn < '2013-02-14'

This insures that you're looking at only date and the time portion is ingored. Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



Not true:



Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



The 'YYYY-MM-DD' string format is affected by the setting of DATEFORMAT. Using 'YYYYMMDD' is not affected by the setting of DATEFORMAT.

I can see where YYYY-MM-DD is affacted by SET LANGUAGE, but I'm not seeing it affacted by SET DATEFORMAT. You're right that YYYYMMDD would be the best for all locations and environments.


"The universe is complicated and for the most part beyond your control, but your life is only as complicated as you choose it to be."
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97708 Visits: 38989
Eric M Russell (2/18/2013)
Lynn Pettis (2/18/2013)
Eric M Russell (2/18/2013)
SQL Server doesn't store date/times values in a format, it's just a typed integer.

However, SET DATEFORMAT setting influences how date/time values are displayed in a resultset and how strings are converted to date/time values. Also, when using the DateTimeOffset datatype, time zone offset comes into play.

When comparing a date/time value to a range, I'd reccomend doing it like this rather than doing a BETWEEN:

WHERE DateColumn >= '2012-08-01' AND DateColumn < '2013-02-14'

This insures that you're looking at only date and the time portion is ingored. Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



Not true:



Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



The 'YYYY-MM-DD' string format is affected by the setting of DATEFORMAT. Using 'YYYYMMDD' is not affected by the setting of DATEFORMAT.

I can see where YYYY-MM-DD is affacted by SET LANGUAGE, but I'm not seeing it affacted by SET DATEFORMAT. You're right that YYYYMMDD would be the best for all locations and environments.


Run this:



declare @DateVal datetime;
set dateformat mdy;
set @DateVal = '2013-02-14';
select @DateVal;
go
declare @DateVal datetime;
set dateformat dmy;
set @DateVal = '2013-02-14';
select @DateVal;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221006 Visits: 42002
Lynn Pettis (2/18/2013)
Eric M Russell (2/18/2013)
Lynn Pettis (2/18/2013)
Eric M Russell (2/18/2013)
SQL Server doesn't store date/times values in a format, it's just a typed integer.

However, SET DATEFORMAT setting influences how date/time values are displayed in a resultset and how strings are converted to date/time values. Also, when using the DateTimeOffset datatype, time zone offset comes into play.

When comparing a date/time value to a range, I'd reccomend doing it like this rather than doing a BETWEEN:

WHERE DateColumn >= '2012-08-01' AND DateColumn < '2013-02-14'

This insures that you're looking at only date and the time portion is ingored. Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



Not true:



Also, the string format YYYY-MM-DD is ISO standard and always translates properly regardless of local language or DATEFORMAT setting.



The 'YYYY-MM-DD' string format is affected by the setting of DATEFORMAT. Using 'YYYYMMDD' is not affected by the setting of DATEFORMAT.

I can see where YYYY-MM-DD is affacted by SET LANGUAGE, but I'm not seeing it affacted by SET DATEFORMAT. You're right that YYYYMMDD would be the best for all locations and environments.


Run this:



declare @DateVal datetime;
set dateformat mdy;
set @DateVal = '2013-02-14';
select @DateVal;
go
declare @DateVal datetime;
set dateformat dmy;
set @DateVal = '2013-02-14';
select @DateVal;
go





OH! Now THAT's interesting! I never even considered that particular format might be affected by DateFormat. Thanks, Lynn. The only place I've ever used the dashed format is for human readability on these very forums. It's amazing I've not run into it before, though.

--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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97708 Visits: 38989
Bit more to think about:



-- datetime data type
declare @DateVal datetime;
set dateformat mdy;
set @DateVal = '2013-02-14';
select @DateVal;
go
declare @DateVal datetime;
set dateformat dmy;
set @DateVal = '2013-02-14';
select @DateVal;
go

declare @DateVal datetime;
set dateformat mdy;
set @DateVal = '2013-14-02';
select @DateVal;
go
declare @DateVal datetime;
set dateformat dmy;
set @DateVal = '2013-14-02';
select @DateVal;
go

-- SQL Server 2008 and newer, Date and Datetime2 data types
declare @DateVal date;
set dateformat mdy;
set @DateVal = '2013-02-14';
select @DateVal;
go
declare @DateVal datetime2;
set dateformat dmy;
set @DateVal = '2013-02-14';
select @DateVal;
go
declare @DateVal datetime2;
set dateformat mdy;
set @DateVal = '2013-02-14';
select @DateVal;
go
declare @DateVal datetime2;
set dateformat dmy;
set @DateVal = '2013-02-14';
select @DateVal;
go

declare @DateVal date;
set dateformat mdy;
set @DateVal = '2013-14-02';
select @DateVal;
go
declare @DateVal datetime2;
set dateformat dmy;
set @DateVal = '2013-14-02';
select @DateVal;
go
declare @DateVal datetime2;
set dateformat mdy;
set @DateVal = '2013-14-02';
select @DateVal;
go
declare @DateVal datetime2;
set dateformat dmy;
set @DateVal = '2013-14-02';
select @DateVal;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Polymorphist
Polymorphist
Old Hand
Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)Old Hand (357 reputation)

Group: General Forum Members
Points: 357 Visits: 269
LOL, glad I could help facilitate some education on date formats and querying. I think this is one of the more confusing areas of querying data.

I'm also mostly an SSRS developer. I do put queries together for use in reports (obviously), but I also lack when it comes to solid DBA skills and foundation. I've taken quite a few courses regarding business intelligence at a local community college and this has helped keep me employed, but one thing i've noticed is that most college courses don't spend a lot of time on the DBA side of development which I think is wrong. It's valuable to know about execution plans and how to optimize queries, that should be a solid foundation. However, I've had to learn most of that stuff on-the-fly and on the job. I also don't know a lot of what I probably should know regarding dates.

So, i'm seeking a course or courses in database administration.

Thanks All, for responding on my thread. You're all a great help.
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53299 Visits: 21202
And here's a really obvious (yeah right!) way of making Lynn's example work:

declare @DateVal datetime;
set dateformat mdy;
set @DateVal = '2013-02-14T00:00:00';
select @DateVal;
go
declare @DateVal datetime;
set dateformat dmy;
set @DateVal = '2013-02-14T00:00:00';
select @DateVal;
go




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
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