June 13, 2012 at 9:12 am
HI Everyone
I have something simple that I am getting stumped on.
I have a complete date with time (2012-06-12 10:43:37), and I need only the date portion. I have used DATEPART() to separate the Year, Month and Day. But I am having difficulty putting those values back together so that I come up with a correct data type of datetime (2012-06-12 00:00:00), or even just the date (2012-06-12)
I am close, but I am coming up with a date that is not correct.
Thank you in advance for your assistance
Andrew SQLDBA
June 13, 2012 at 9:14 am
AndrewSQLDBA (6/13/2012)
HI EveryoneI have something simple that I am getting stumped on.
I have a complete date with time (2012-06-12 10:43:37), and I need only the date portion. I have used DATEPART() to separate the Year, Month and Day. But I am having difficulty putting those values back together so that I come up with a correct data type of datetime (2012-06-12 00:00:00), or even just the date (2012-06-12)
I am close, but I am coming up with a date that is not correct.
Thank you in advance for your assistance
Andrew SQLDBA
SELECT CAST(yourdatetimecolumn AS DATE)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 13, 2012 at 9:21 am
Thank You
I was trying to go about that the long way around of taking each part and concatenating it back together and then cast it to the correct data type.
Thanks for your help
Andrew SQLDBA
June 13, 2012 at 9:24 am
ChrisM@Work (6/13/2012)
AndrewSQLDBA (6/13/2012)
HI EveryoneI have something simple that I am getting stumped on.
I have a complete date with time (2012-06-12 10:43:37), and I need only the date portion. I have used DATEPART() to separate the Year, Month and Day. But I am having difficulty putting those values back together so that I come up with a correct data type of datetime (2012-06-12 00:00:00), or even just the date (2012-06-12)
I am close, but I am coming up with a date that is not correct.
Thank you in advance for your assistance
Andrew SQLDBA
SELECT CAST(yourdatetimecolumn AS DATE)
And for those reading this and not getting this to work because you are still running SQL Server 2005 (or earlier):
select dateadd(dd,datediff(dd,0,yourdatecolumn),0)
June 13, 2012 at 9:37 am
SET DATEFORMAT dmy;
GO
SELECT
[Getdate] = GETDATE()
,[Year] = DATEPART ( yy , GETDATE() )
,[Month] = DATEPART ( mm , GETDATE() )
,[Day] = DATEPART ( dd , GETDATE() )
,[and back again] = CAST ( CAST(DATEPART ( dd , GETDATE() ) AS VARCHAR(4))+'.'+ CAST (DATEPART ( mm , GETDATE() )AS VARCHAR(2))+'.'+CAST (DATEPART ( yy , GETDATE() )AS varCHAR(4)) AS DATETIME)
June 13, 2012 at 9:43 am
440692 I am just a number (6/13/2012)
SET DATEFORMAT dmy;
GO
SELECT
[Getdate] = GETDATE()
,[Year] = DATEPART ( yy , GETDATE() )
,[Month] = DATEPART ( mm , GETDATE() )
,[Day] = DATEPART ( dd , GETDATE() )
,[and back again] = CAST ( CAST(DATEPART ( dd , GETDATE() ) AS VARCHAR(4))+'.'+ CAST (DATEPART ( mm , GETDATE() )AS VARCHAR(2))+'.'+CAST (DATEPART ( yy , GETDATE() )AS varCHAR(4)) AS DATETIME)
A lot of unnecessary work for what can be doe using either of the following:
SELECT CAST(yourdatetimecolumn AS DATE)
select dateadd(dd,datediff(dd,0,yourdatecolumn),0)
June 13, 2012 at 9:46 am
Lynn Pettis (6/13/2012)
ChrisM@Work (6/13/2012)
AndrewSQLDBA (6/13/2012)
HI EveryoneI have something simple that I am getting stumped on.
I have a complete date with time (2012-06-12 10:43:37), and I need only the date portion. I have used DATEPART() to separate the Year, Month and Day. But I am having difficulty putting those values back together so that I come up with a correct data type of datetime (2012-06-12 00:00:00), or even just the date (2012-06-12)
I am close, but I am coming up with a date that is not correct.
Thank you in advance for your assistance
Andrew SQLDBA
SELECT CAST(yourdatetimecolumn AS DATE)
And for those reading this and not getting this to work because you are still running SQL Server 2005 (or earlier):
select dateadd(dd,datediff(dd,0,yourdatecolumn),0)
Whether you're stuck in a timewarp or not, it's well worth spending some time on select dateadd(dd,datediff(dd,0,yourdatecolumn),0) to properly understand it. It's a tremendously useful piece of code.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 13, 2012 at 9:49 am
June 13, 2012 at 9:52 am
jeffem (6/13/2012)
Though the answers have already been shared, it can be useful to have the options for CONVERT() available at your disposal. This link[/url] can be a useful primer...
Only if you're wearing your sunnies. It's ghastly!
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 13, 2012 at 9:54 am
ChrisM@Work (6/13/2012)
jeffem (6/13/2012)
Though the answers have already been shared, it can be useful to have the options for CONVERT() available at your disposal. This link[/url] can be a useful primer...Only if you're wearing your sunnies. It's ghastly!
Isn't it?! It's like somebody got a copy of HTML For Dummies from 1996.
June 13, 2012 at 10:02 am
440692 I am just a number (6/13/2012)
@LynnI absolutely agree,
but (there had to be one didn't there 🙂 )
The OP did ask about how to concatenating the values back
And we showed a better way rather than breaking apart and concatenating, which is much slower.
June 13, 2012 at 12:45 pm
... Check out Lynn's date/time conversions blog post. The link is in my signature block below.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply