Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Select Datepart Expand / Collapse
Author
Message
Posted Monday, June 19, 2006 2:12 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, April 5, 2013 4:27 AM
Points: 95, Visits: 47

Is there a way for me to use the Datepart function so that it would return today's date as:

20060619

Or is there another way I can do it?

The below didn't quite get me what I needed.

SELECT DATEPART(YYYY, GETDATE()), DATEPART(MM,GETDATE()) , DATEPART(DD, GETDATE())

Thanks!

Post #288619
Posted Monday, June 19, 2006 2:22 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 5:12 PM
Points: 4,000, Visits: 4,360
print convert(varchar,@datetime,112)



John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Post #288621
Posted Monday, June 19, 2006 2:38 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 25, 2016 12:56 PM
Points: 392, Visits: 283
Yes, there is. This code will do what you need:

SELECT
Cast(Datepart(yyyy, GetDate()) AS char(4)) +
Right('0' + Cast(DatePart(mm, GetDate()) AS Varchar(2)), 2) +
Right('0' + Cast(DatePart(dd, GetDate()) AS Varchar(2)), 2)




Post #288625
Posted Monday, June 19, 2006 2:41 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, April 5, 2013 4:27 AM
Points: 95, Visits: 47

That worked great, thanks!

Post #288626
Posted Monday, June 19, 2006 2:53 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 5:12 PM
Points: 4,000, Visits: 4,360

I just realized that I pasted the wrong section into my post.

This returns the same result as Brendt's DatePart/Cast example, but with less function calls:

SELECT CONVERT(varchar,GETDATE(),112)





John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Post #288632
Posted Monday, June 19, 2006 3:28 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 25, 2016 12:56 PM
Points: 392, Visits: 283
Perhaps, but the questioner specifically requested a method using DatePart. I use this when generating a YYYYMMDDHHNN strings, in preference to stripping formatting out of a CONVERTed datetime, because it is far more readable.


Post #288642
Posted Monday, June 19, 2006 3:29 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, January 25, 2016 12:56 PM
Points: 392, Visits: 283
Of course, I never noticed the "Or is there some other way to do it" portion of the post 8-O


Post #288644
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse