October 2, 2009 at 9:52 am
I have a datetime field that I want to convert to text in another field and also have the full name of the month displayed along with dropping any preceeding zeroes in the date that it returns.
If I run the following:
Update membership_table
set membership_period = convert(varchar, effective_date, 107)
Note: membership_period is varchar
It returns the following results in the field in the two records I'm testing:
Jan 01, 2010
Nov 01, 2009
In the examples above (and any others that I'll need to run it against) I want the months spelled out (Jan - January, Feb-February, Nov-November, etc) and I'd also like the actual dates not to be preceeded by a 0. So, Jan 01, 2010 would actually need to be January 1, 2010.
Can someone point me in the right direction?
Thank you.
Roger
October 2, 2009 at 10:48 am
The DatePart and DateName functions should handle what you need to do.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
October 2, 2009 at 4:02 pm
I agree with Wayne and I'll go a bit further. Study ALL of the functions in SQL Server. At least learn what they do so you know what is possible... especially the date functions.
To get you out of the woods, here's an example...
SELECT DATENAME(mm,GETDATE())+' '+DATENAME(dd,GETDATE())+', '+DATENAME(yy,GETDATE())
The thing I'd really like to know is, considering that date formatting is normally considered to be a form of "Death by SQL", why do you need to format the dates? Where will the formatted dates be used?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 3, 2009 at 2:06 am
Thanks everyone for pointing me in the right direction. I did figure it out. I am setting it up to use in the body of an email where I need the dates printed in text format... Like January 1, 2010.
Rog
October 3, 2009 at 10:51 am
Roger Abram (10/3/2009)
I did figure it out.
Would you mind posting what your final code is? Thanks.
I am setting it up to use in the body of an email where I need the dates printed in text format... Like January 1, 2010.
Thanks for the feedback on that. I always wonder...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply