Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Weird Question About Querying with Dates Expand / Collapse
Author
Message
Posted Sunday, February 17, 2013 1:26 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, June 2, 2014 1:21 AM
Points: 25, Visits: 163
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!!
Post #1420948
Posted Sunday, February 17, 2013 2:20 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, June 2, 2014 1:21 AM
Points: 25, Visits: 163
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.
Post #1420951
Posted Monday, February 18, 2013 9:03 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, October 16, 2014 10:37 AM
Points: 1,702, Visits: 4,837
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.

Post #1421251
Posted Monday, February 18, 2013 9:12 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:53 PM
Points: 20,727, Visits: 32,485
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.



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)
Post #1421259
Posted Monday, February 18, 2013 9:42 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, October 16, 2014 10:37 AM
Points: 1,702, Visits: 4,837
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.
Post #1421282
Posted Monday, February 18, 2013 9:46 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:53 PM
Points: 20,727, Visits: 32,485
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





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)
Post #1421283
Posted Monday, February 18, 2013 4:54 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:45 PM
Points: 35,342, Visits: 31,875
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1421378
Posted Monday, February 18, 2013 5:30 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:53 PM
Points: 20,727, Visits: 32,485
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





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)
Post #1421388
Posted Monday, February 18, 2013 5:43 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, June 2, 2014 1:21 AM
Points: 25, Visits: 163
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.
Post #1421392
Posted Tuesday, February 19, 2013 5:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 5,147, Visits: 11,989
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1421572
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse