January 15, 2015 at 9:41 am
I have a date filed - an example of what it pulls back is -
2013-05-10 08:15:24.000
I want it to just return - 2013-05-10 00:00:00.000, so the time is ignored altogether.
What do I need in my select if my select is similar to below -
Select datefield
from datetable
Thanks
January 15, 2015 at 9:45 am
TSQL Tryer (1/15/2015)
I have a date filed - an example of what it pulls back is -2013-05-10 08:15:24.000
I want it to just return - 2013-05-10 00:00:00.000, so the time is ignored altogether.
What do I need in my select if my select is similar to below -
Select datefield
from datetable
Thanks
Select datefield = cast(datefield as date)
from datetable
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
January 15, 2015 at 9:48 am
Try:
Select dateadd(day, datediff(day, 0, datefield), 0) as datefield
from datetable
Hope this helps.
January 15, 2015 at 10:06 am
imex (1/15/2015)
Try:
Select dateadd(day, datediff(day, 0, datefield), 0) as datefield
from datetable
Hope this helps.
This is a waste of CPU cycles.
Stick with Phil Parkins suggestion. Stick to the KISS principle when possible.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
January 15, 2015 at 10:09 am
Try:
Select dateadd(day, datediff(day, 0, datefield), 0) as datefield
from datetable
Hope this helps.
http://www.imoveisemexposicao.com.br[/quote%5D
Same execution plan for my solution, though yours does return the zero time component & is therefore a more exact match to requirements 😀
Edit: Oops, quoted wrong post on original reply.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
January 15, 2015 at 10:17 am
Phil Parkin (1/15/2015)
Try:
Select dateadd(day, datediff(day, 0, datefield), 0) as datefield
from datetable
Hope this helps.
http://www.imoveisemexposicao.com.br[/quote%5D
Same execution plan for my solution, though yours does return the zero time component & is therefore a more exact match to requirements 😀
Edit: Oops, quoted wrong post on original reply.
As this is in a SQL Server 2012 forum, we can make an assumption that the OP is using SQL Server 2012. With that, Phil, your solution is quite valid. If you assign the result of your solution to a datetime variable or column, it will be converted back to a datetime value with a zero time component. This solution will also wotk in SQL Server 2008. The dateadd solution is valid for SQL Server 2005 and older.
January 15, 2015 at 12:09 pm
SELECT CAST(CAST(datefield AS DATE) AS DATETIME)
FROM datetable
January 15, 2015 at 12:24 pm
Prudhvi _SQL (1/15/2015)
SELECT CAST(CAST(datefield AS DATE) AS DATETIME)FROM datetable
Why stop there?
How about
select cast((stuff(stuff(convert(char(8), datefield, 112), 5, 0, '-'), 8, 0, '-')) as datetime)
?:hehe:
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
January 16, 2015 at 2:19 am
Cheers everyone.
January 16, 2015 at 8:48 am
TSQL Tryer (1/15/2015)
I have a date filed - an example of what it pulls back is -2013-05-10 08:15:24.000
I want it to just return - 2013-05-10 00:00:00.000, so the time is ignored altogether.
What do I need in my select if my select is similar to below -
Select datefield
from datetable
Thanks
I see a potential SARGability problem that will kill performance here. Could you post the actual query that you're trying to build?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply