SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cast varchar number into Date Format
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Johnph
Yak Posting Veteran

95 Posts

Posted - 07/12/2013 :  14:18:48  Show Profile  Reply with Quote
I have a column (varchar (50)), It has values from 1-12.

I want to cast them to their appropriate months.

Example: 1 = January
2 = Febuary
3 = March
etc.

James K
Flowing Fount of Yak Knowledge

3567 Posts

Posted - 07/12/2013 :  14:28:08  Show Profile  Reply with Quote
You can use a case expression like this:
CASE yourVarcharCol
	WHEN '1' THEN 'January'
	WHEN '2' THEN 'February'
	---  etc
END AS MonthName;
Alternatively, if you want to make it hard for a non-SQL person to read and understand, you can do the following:
DATENAME(month,DATEADD(mm,CAST(@yourcol AS INT)-1,0))
Go to Top of Page

Johnph
Yak Posting Veteran

95 Posts

Posted - 07/12/2013 :  14:42:18  Show Profile  Reply with Quote
Thanks again Jim, I prefer the 2nd method... makes me look like I know what I'm doing.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3567 Posts

Posted - 07/12/2013 :  15:03:39  Show Profile  Reply with Quote
Of course! I must admit that I myself have used "simplified" queries similar to that to pretend to be a know-it-all and to watch people's jaws drop.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000