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

Month and Day of Datepart Expand / Collapse
Author
Message
Posted Wednesday, January 04, 2006 8:00 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, April 11, 2014 8:37 AM
Points: 57, Visits: 140

Hi,

 

I need to get a two digit day and month out of the datepart and it's only bringing back one digit.

 

select datepart(mm,getdate())

 

brings back 1 instead of 01.

 

Any advice will be appreciated.

 

Thanks,




Post #248107
Posted Wednesday, January 04, 2006 9:07 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 03, 2007 2:55 AM
Points: 928, Visits: 1
I posted the code for this at another site.

See this link.

http://sqlobserver.com/community/index.php?topic=57.0



------------------------------
The Users are always right - when I'm not wrong!
Post #248142
Posted Thursday, January 05, 2006 2:24 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, April 03, 2008 6:45 AM
Points: 247, Visits: 26
select right('0' + convert(varchar,datepart(mm,getdate())),2)



Post #248344
Posted Thursday, January 05, 2006 8:40 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:45 AM
Points: 977, Visits: 193

select substring(convert(char(10),getdate(),101),1,2)

Post #248467
Posted Thursday, January 05, 2006 8:44 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, April 11, 2014 8:37 AM
Points: 57, Visits: 140

This is what I did but I think it stinks that the datepart function won't return the two digits even though you specify mm or dd.  Isn't that the reason for what instead of using m or d.

 

I had to rant a little. Sorry




Post #248473
Posted Thursday, January 05, 2006 11:05 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, March 25, 2014 9:21 AM
Points: 26, Visits: 76
Well, it is perfectly logical that it doesn't return a leading 0.   The Datepart returns an Integer value.   Integers are usually not expressed with leading 0s!  Which is why the conversion to a string is nescessary.
Post #248558
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse