views:

1261

answers:

6

I want to save my dates in sql2005 as dates (without hour minutes and seconds). I want to do this because the between function isn't always correct if the hours, minutes and seconds are filled in.

but neither datetime nor smalldatetime allows this, in 2008 you have the Date column which can do this.

I've also found this question which helps me out but I don't like the casting to execute my queries: http://stackoverflow.com/questions/2775/whats-the-best-way-to-remove-the-time-portion-of-a-datetime-value-sql-server

A: 

If all else fails you could store it as an integer value of seconds from a point in time. This would allow you to compare it against other dates stored the same way without casting.

Element
You don't need to write any complex code to do this - simply cast(DateTimeValue as int).
Ed Harper
A: 
SELECT CONVERT (date, GETDATE()) -- Without hours, minutes and seconds.

For more details, please refer to http://msdn.microsoft.com/en-us/library/ms188383.aspx

StartClass0830
this only works in sql 2008
Sem Dendoncker
A: 

If you're working in .NET you can use

DateTime.Now.Date

to give you just the date part of a datetime.

Neil Barnwell
indeed but most of my querying is done within my stored procedures ... i guess casting will be the only way to get over this ...
Sem Dendoncker
Yes that's fair enough. I did make a big assumption that this might be suitable.
Neil Barnwell
+6  A: 

The non-casting way:

SET @d = DATEADD(dd, 0, DATEDIFF(dd, 0, @dt))

This adds the number of days between 0 and your date to 0, resulting in the time-part to be zero.

If that is faster than the explicit CAST approach must be tested. The use of 0 suggests some implicit casting will be done in this expression.

Tomalak
indeed, the tests tell me that the casting way is still the fastest approach, but thx for the explanation.
Sem Dendoncker
@Sem The casting way is NOT faster. Please see [this post for more detail](http://stackoverflow.com/questions/2775/whats-the-best-way-to-remove-the-time-portion-of-a-datetime-value-sql-server/3696991#3696991).
Emtucifor
Hi @Emtucifor, it really doesn't matter anymore. SQL 2008 has the field "Date" which solves this problem flawlessly ;). But thank you for your comment.
Sem Dendoncker
@Sem For those lucky enough to be off SQL 2005 or even SQL 2000... :)
Emtucifor
@Emtucifor: you are right, we had to wait untill 2 weeks ago untill all of our apps were on 2008. Good luck and +1 on your comment!
Sem Dendoncker
@Tomalak I don't think an implicit cast on a literal is any slower than an explicit cast.
Emtucifor
+1  A: 

What you could do is make sure that the dates being stored do not have minutes and seconds BEFORE you send them to the database. This should be pretty simple from your application code if you create a datetime based on an existing datetime but set the hours, minutes and seconds to zero

Conrad
you are right, but there are allready dates stored now.
Sem Dendoncker
A: 

BETWEENs won't work because the datetime has time other than 00:00:00.000, so fix the BETWEENS, not the data.

This will return all data on a single day, regardless of time

DECLARE @FilterDate datetime
SET @FilterDate='3/18/2009'

QUERY
    ...
    WHERE Column1 >= @FilterDate AND Column1 < @FilterDate + 1

This will return all data on a date range, regardless of time

DECLARE @StartFilterDate datetime
DECLARE @EndFilterDate datetime
SET @StartFilterDate ='3/18/2009'
SET @EndFilterDate ='3/30/2009'

QUERY
    ...
    WHERE Column1 >= @StartFilterDate AND Column1 < @EndFilterDate + 1
KM
this is what i'm doing now but i don't like the way of working. i simply don't need the hour, minutes and seconds
Sem Dendoncker