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 ««12

convert month name to month number Expand / Collapse
Author
Message
Posted Thursday, May 6, 2010 10:31 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:08 PM
Points: 3,132, Visits: 11,429
This is fairly simple:
select
*,
MonthNumber = month([month]+' 1 2010')
from
( --test data
select [month] = 'January' union all
select [month] = 'April' union all
select [month] = 'September' union all
select [month] = 'December'
) a


Results:
month     MonthNumber
--------- -----------
January 1
April 4
September 9
December 12

(4 row(s) affected)
Post #917595
Posted Friday, May 7, 2010 3:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 7:55 PM
Points: 197, Visits: 595
how abt this?

declare @month datetime
set @month = 'apr'+'1 2010'
SELECT DATEPART(MONTH, @month)

Cheers,,,


Cheers,
Ankur
Post #917725
Posted Saturday, June 11, 2011 12:43 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, June 12, 2014 10:23 PM
Points: 53, Visits: 911
Jeff Moden (5/6/2010)
[quote]
There've been a lot of answers to this question and they could all be unnecessary because SQL Server is very forgiving for most date formats. With that thought in mind, what does the original data look like? And I'm not just talking about the month name. For example, if your trying to convert a string to an SQL Datetime and the date looks like '1 June 2010', there you don't need to find the month number to do the conversion....

SELECT CAST('4 June 2010' AS DATETIME)




Your are genious , Jeff Moden

I like your simplicity.
Post #1123763
Posted Saturday, June 11, 2011 8:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:40 PM
Points: 6,582, Visits: 8,864
Dehqon D. (6/11/2011)
Your are genious , Jeff Moden

I like your simplicity.


Agreed. Jeff has a way with cutting to the chase and seeing what is really needed.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1123794
Posted Friday, December 28, 2012 2:22 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, July 28, 2014 5:28 AM
Points: 321, Visits: 570
I've just found that this works in Reporting Services as well where Fields!Season_Month returns the month name only:

=Month("1 " & Fields!Season_Month.Value & " 2012")




SQL SERVER Central Forum Etiquette
Post #1400839
Posted Wednesday, January 2, 2013 3:39 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:45 PM
Points: 3,617, Visits: 5,237
Very forgiving indeed.

SELECT MONTH('April' + ' 1900')





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1401738
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse