|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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))
|
|
#4
|
|||
|
|||
|
Quote:
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? |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
I meant in SQL Server, not sure if you thought I meant otherwise. Anyway, thx man.
|
|
#7
|
||||
|
||||
|
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 |
|
#8
|
|||
|
|||
|
I don't know, nevermind, I'm complicated...sorry
![]() |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > DateTime: Zeroing the time portion |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|