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

datetime check constraint for 23:59:59.997 Expand / Collapse
Author
Message
Posted Wednesday, August 20, 2014 10:44 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Yesterday @ 4:34 PM
Points: 20,676, Visits: 32,269
Eric M Russell (8/20/2014)
dirk.dromgoole (8/20/2014)
I suppose there are probably better ways to modify the query. Basically, the WHERE clause of the query is:
WHERE (GETDATE() - 1 <= EndDate)
If EndDate has a time of 00:00:00.000 or any time for that matter that is not the end of the day then that whole day is not calculated. EndDate with a time of 00:00:00.000 will be less than GETDATE()-1 so the whole day before will not be accounted for.


If you don't need time portion, then use Date datatype instead of DateTime. If you're stuck with existing columns of type DateTime and just need to restrict data entered, then you can implement a check constraint on the column. Comparing value entered to a re-cast of itself is an easy and reliable way to implement this specific type of check. For example:

create table #t 
(
EndDate datetime
constraint ck_EndDate check (EndDate = cast(EndDate as date))
);

insert into #t ( EndDate ) values ('2014/08/20');

(1 row(s) affected)


insert into #t ( EndDate ) values ('2014/08/20 20:59:59.997');

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the CHECK constraint "ck_EndDate".




As this was posted in a SQL Server 2005 forum the DATE data type is not available in this version of SQL Server.



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 #1605523
Posted Wednesday, August 20, 2014 11:24 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, September 19, 2014 1:39 PM
Points: 1,660, Visits: 4,748
Lynn Pettis (8/20/2014)

As this was posted in a SQL Server 2005 forum the DATE data type is not available in this version of SQL Server.


The following check constraint should work in a similar way under SQL Server 2005.

create table #t 
(
EndDate datetime
constraint ck_EndDate check (EndDate = convert(char(8),EndDate,112))
);

Post #1605542
Posted Wednesday, August 20, 2014 5:44 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Yesterday @ 1:47 PM
Points: 35,215, Visits: 31,667
Sean Lange (8/20/2014)
BTW, You should not get into the habit of things like getdate() - 1. You should be explicit and use dateadd.


Heh... or just dump the newer date/time datatypes so that you can take these types of wonderful shortcuts.


--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 #1605639
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse