Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

datetime => how to have complete date with 00:00:00.000 Expand / Collapse
Author
Message
Posted Wednesday, December 2, 2009 1:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 3, 2010 5:57 AM
Points: 18, Visits: 32
Hi,

i work with sql server 2005 and i 'm a beginner
I have a table with a column datetime.

in this table i have some date as this :

2008-10-28 11:04:04.207
2009-03-30 13:26:10.433
2009-01-16 14:06:20.033

but i'd like when i do a query select * from myTable to have the date in format

2008-10-28 00:00:00.000
2009-03-30 00:00:00.000
2009-01-16 00:00:00.000

How i can do this !?

Thanks in advance
Christophe
Post #827249
Posted Wednesday, December 2, 2009 1:57 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, February 24, 2014 1:01 AM
Points: 503, Visits: 610
A number of ways of doing this:

SELECT CONVERT(datetime,CONVERT(char(8),MyDateTimeColumn,112))
FROM MyTable

or

SELECT CONVERT(datetime,DATEDIFF(DD,0,MyDateTimeColumn))
FROM MyTable

Hope that helps.

Mike
Post #827252
Posted Wednesday, December 2, 2009 1:59 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, September 12, 2014 6:43 AM
Points: 1,113, Visits: 3,125
One way I often do this:

CREATE TABLE #MyTable
(
MyDate DATETIME
)

INSERT INTO #MyTable
SELECT '2008-10-28 11:04:04.207' UNION ALL
SELECT '2009-03-30 13:26:10.433' UNION ALL
SELECT '2009-01-16 14:06:20.033'

SELECT MyDate,
DATEADD(dd, DATEDIFF(dd, 0, MyDate), 0) MyDate_DateOnly
FROM #MyTable

DROP TABLE #MyTable


Hope this helps.

Cheers,
Simon
Post #827254
Posted Wednesday, December 2, 2009 2:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, August 3, 2010 5:57 AM
Points: 18, Visits: 32
Hi both,

thanks for your sample, it's ok now !

thanks for your time
christophe
Post #827266
Posted Wednesday, December 2, 2009 9:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,032, Visits: 5,283
Here is a link to some common date routines:
http://www.sqlservercentral.com/blogs/lynnpettis/archive/2009/03/25/some-common-date-routines.aspx

CEWII
Post #827619
Posted Wednesday, December 2, 2009 7:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
Be a bit careful about using character conversions of dates, folks. They're s-l-o-w. Might not make any difference with just a couple of thousand rows but if you work with millions of rows, it can really add up in a hurry.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #827891
Posted Thursday, April 7, 2011 9:20 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:38 PM
Points: 3,931, Visits: 7,160
Jeff, what do you suggest as a best-practice for returning a date with 00:00:00.000?

______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience"
Post #1089997
Posted Thursday, April 7, 2011 2:57 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
Either of the following is what I consider to be a "best practice". They're both very fast but the first one edges out the second when it comes to millions of rows if you need to apply it to something that large.
 SELECT CAST(DATEDIFF(dd,0,GETDATE()) AS DATETIME),
DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)

Replace the GETDATE() with whatever date you may have including dates in a column from a table. Just don't forget to add the FROM clause for the table.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1090254
Posted Friday, April 8, 2011 5:02 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 2:42 AM
Points: 1,167, Visits: 2,652
You may find this useful too:

http://www.simple-talk.com/sql/learn-sql-server/robyn-pages-sql-server-datetime-workbench/


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #1090501
Posted Friday, April 8, 2011 8:02 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
Jeff Moden (4/7/2011)
Either of the following is what I consider to be a "best practice". They're both very fast but the first one edges out the second when it comes to millions of rows if you need to apply it to something that large.
 SELECT CAST(DATEDIFF(dd,0,GETDATE()) AS DATETIME),
DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)

Replace the GETDATE() with whatever date you may have including dates in a column from a table. Just don't forget to add the FROM clause for the table.


Ah... I just had a horrible thought based on how the original question was posed. If you're trying to use such a thing as criteria for a JOIN or a WHERE clause, DO NOT use either of the two methods above on the COLUMN of a TABLE. Instead, do this...

 SELECT something
FROM SomeTable
WHERE SomeDateColumn >= SomeWholeDate
AND SomeDateColumn < DATEADD(dd,1,SomeWholeDate)


The code above finds all the rows in SomeTable that have a date (with or without time) that falls on SomeWholeDate and it does so while still allowing an index on SomeDateColumn to be used. Using just about any formula on SomeDateColumn itself will prevent the use of an index.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1090651
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse