dbTalk Databases Forums  

check if a DateTime value is over midnight

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss check if a DateTime value is over midnight in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Fiaola
 
Posts: n/a

Default check if a DateTime value is over midnight - 10-28-2010 , 06:29 PM






I'm trying to write an SQL Query in SQL Server 2008 to update a date
field in my table. The field is a DATETIME field type. When a record
is inserted and it is over midnight (lets' say between MIDNIGHT 00:00
AM AND 03:00 AM, i want to store the previous Date.

Any help would be appreciated. Thanks in ADVANCE

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: check if a DateTime value is over midnight - 10-29-2010 , 02:21 AM






Fiaola (teeleaf96799 (AT) gmail (DOT) com) writes:
Quote:
I'm trying to write an SQL Query in SQL Server 2008 to update a date
field in my table. The field is a DATETIME field type. When a record
is inserted and it is over midnight (lets' say between MIDNIGHT 00:00
AM AND 03:00 AM, i want to store the previous Date.

Any help would be appreciated. Thanks in ADVANCE
The dateadd function might help you. Unfortunately, I don't want to say
more than so, because I don't understand what you are looking for. Could
you give an example with the starting point and what you want the final
result to be?


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #3  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: check if a DateTime value is over midnight - 10-30-2010 , 01:28 PM



Fiaola wrote:
Quote:
I'm trying to write an SQL Query in SQL Server 2008 to update a date
field in my table. The field is a DATETIME field type. When a record
is inserted and it is over midnight (lets' say between MIDNIGHT 00:00
AM AND 03:00 AM, i want to store the previous Date.

Any help would be appreciated. Thanks in ADVANCE
If you are only interesting in storing the date portion, then you could
use this:

CREATE TABLE #t (my_datetime_column datetime)
INSERT INTO #t VALUES ('20101030 11:00')
INSERT INTO #t VALUES ('20101027 02:30')
INSERT INTO #t VALUES ('20101021 05:00')
INSERT INTO #t VALUES ('20101015 23:59')

SELECT my_datetime_column
, DATEADD(day, DATEDIFF(day, 0, DATEADD(hour, -3,
my_datetime_column)), 0)
FROM #t

DROP TABLE #t

--
Gert-Jan

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.