MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 8th, 2004, 06:37 AM
FlashHeart FlashHeart is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: UK
Posts: 78 FlashHeart User rank is Lance Corporal (50 - 100 Reputation Level)FlashHeart User rank is Lance Corporal (50 - 100 Reputation Level)FlashHeart User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 27 sec
Reputation Power: 5
DateTime: Zeroing the time portion

Hi,

Any idea how I can zeroize the time part of a DateTime value?
I have a script which retrieves the current date and adds 4 hours to it like so:-

declare @Date datetime;
set @Date = dateadd(hour, 12, getdate());

The trouble is that after this the value of @Date might be equal to:

2004-05-09 12:15:17.793

but the field value that I need to compare this to later on (in a select) has no time part, ie:-

2004-05-09 00:00:00.000


Any idea how I could clear the time part of @Date so the comparison would succeed???

Many Thanks

dex

Reply With Quote
  #2  
Old May 8th, 2004, 07:31 AM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
You can use the date funtions...here's a snip that will show you what it will output. You can possibly use it in your compare criteria.
Code:
Declare @Date datetime;
set     @Date = 2004-05-09 12:15:17.793

Select  DATE(year, @date),
        DATE(month, @date),
        DATE(day, @date)

...or, I've had luck w/being able to extract the yr, mo, day (no min.sec.ms) using the LEFT function, but I think my dates are smallDateTime...1st give it a shot w/getDate(), then try it w/your @Date and see what you get.
Code:
Select  LEFT(getDate(), 11)

Select  LEFT(@date, 11)

...that may spit out precision to the day, and leave out the time stuff.

Last edited by Username=NULL : May 8th, 2004 at 07:36 AM.

Reply With Quote
  #3  
Old May 8th, 2004, 09:23 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,918 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 49 m 12 sec
Reputation Power: 1018
LEFT(getDate(),11) leaves you at the mercy of the default date display format

the "best practice" method of zeroing the time portion of a datetime value is

cast(convert(char(10),datetimecol,120) as datetime)

however, i should warn you, if you perform a comparison like function(datetimecol) comparisonoperator value, chances are good the optimizer will ignore an index on datetimecol

what your query should look like is datetimecol comparisonoperator function(value)

for example, if i were looking for all datetimecol values from yesterday, i would write
Code:
 where datetimecol 
    >= dateadd(day, -1
       , cast(convert(char(10),getdate,120) as datetime))
   and datetimecol 
     < cast(convert(char(10),getdate,120) as datetime))
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old May 8th, 2004, 02:03 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Quote:
Originally Posted by r937
LEFT(getDate(),11) leaves you at the mercy of the default date display format[/code]

ahhh...ok, admittingly I never knew for fact, but had suspected, about the display format. Do you know where in Server I can go to take a look @ that and what options there are?

Also Rudy, do you mind if I take that code you posted and play around w/it?

Reply With Quote
  #5  
Old May 8th, 2004, 03:02 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,918 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 49 m 12 sec
Reputation Power: 1018
server options? sorry, i are not a dba

play around with my code, please

i think there may be an extra parenthesis at the end, the result of hasty cuttin&pastin

Reply With Quote
  #6  
Old May 8th, 2004, 05:30 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
I meant in SQL Server, not sure if you thought I meant otherwise. Anyway, thx man.

Reply With Quote
  #7  
Old May 8th, 2004, 05:57 PM
r937's Avatar
r937 r937 is online now
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,918 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 12 h 49 m 12 sec
Reputation Power: 1018
of course i thought that you meant sql server

what did you think i thought you meant?

this is the sql server forum, after all

Reply With Quote
  #8  
Old May 8th, 2004, 06:56 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
I don't know, nevermind, I'm complicated...sorry

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > DateTime: Zeroing the time portion


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT