SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert month name to month number


convert month name to month number

Author
Message
madhu.gut
madhu.gut
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 78
Hi All
How should i convert the month name to month number like
'April' it should converted to 4
'January' it should converted to 1
in this how should i do this
can any one suggest the correct answer
Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7266 Visits: 8370
there is no inverse of dateName.
I would use a table to do this.



Clear Sky SQL
My Blog
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9202 Visits: 8492
Could you do something like this?

DECLARE @getmonth   TINYINT,
@datestring VARCHAR(20),
@input VARCHAR(20)

SET @input = 'January'
SET @datestring = @input + ' 1 2010'
SET @getmonth = MONTH(CAST(@datestring AS DATETIME))

SELECT @getmonth




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
sharath.chalamgari
sharath.chalamgari
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2006 Visits: 798
the above Query works with short names of the month


Declare @month as varchar(10)
set @Month = 'Dec'
select Month(@month + ' 1 2010')
Henrico Bekker
Henrico Bekker
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7624 Visits: 5016
CASE 'January' when 1 

etc?

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle
s_osborne2
s_osborne2
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1821 Visits: 2292
SELECT  ( CASE yourDate
WHEN 'January' THEN 1
WHEN 'February' THEN 2
WHEN 'March' THEN 3
WHEN 'April' THEN 4
WHEN 'May' THEN 5
WHEN 'June' THEN 6
WHEN 'July' THEN 7
WHEN 'August' THEN 8
WHEN 'September' THEN 9
WHEN 'October' THEN 10
WHEN 'November' THEN 11
WHEN 'December' THEN 12
END )



That should do the trick.



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9202 Visits: 8492
I don't understand why the use of a case when a simple function would do the job. . .

My original suggestion fulfills the requirements: -
DECLARE @getmonth   TINYINT,
@datestring VARCHAR(20),
@input VARCHAR(20)

SET @input = 'January'
SET @datestring = @input + ' 1 2010'
SET @getmonth = MONTH(CAST(@datestring AS DATETIME))

SELECT @getmonth



As does the post below mine which basically shortened the code.

DECLARE @month AS VARCHAR(20) 

SET @Month = 'January'

SELECT MONTH(@month + ' 1 2010')



They'd both also work if you used short names for the months, e.g. "Jan", "Feb" etc. Making them more efficient than a Case, since you'd need to have 24 arguments to replicate it.

Unless I'm missing something?


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7266 Visits: 8370
skcadavre (5/6/2010)
I don't understand why the use of a case when a simple function would do the job



Try both methods over say 1 million rows.
I would suspect , though i havent tried, that the overhead of the date and then the extraction of the month number would be quite high.

In any case calling a Function (scalar udf) a million time is bad news.

http://sqlblogcasts.com/blogs/sqlandthelike/archive/2009/10/15/udf-overhead-a-simple-example.aspx



Clear Sky SQL
My Blog
Cadavre
Cadavre
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9202 Visits: 8492
Dave Ballantyne (5/6/2010)
In any case calling a Function (scalar udf) a million time is bad news.

http://sqlblogcasts.com/blogs/sqlandthelike/archive/2009/10/15/udf-overhead-a-simple-example.aspx


That was a very interesting read, thankyou Smile

This is my random months table - 1,000,000 rows (not the cleverest way to do it, just wanted to grab some test data)
USE testingdb
IF EXISTS (SELECT *
FROM sys.tables t
JOIN sys.schemas s
ON t.schema_id = s.schema_id
WHERE t.[Name] = 'monthsNO'
AND s.[Name] = 'dbo')
DROP TABLE dbo.monthsno
CREATE TABLE dbo.monthsno
(
[fldmonth] TINYINT NOT NULL
)
ON [PRIMARY]
GO

USE testingdb
IF EXISTS (SELECT *
FROM sys.tables t
JOIN sys.schemas s
ON t.schema_id = s.schema_id
WHERE t.[Name] = 'months'
AND s.[Name] = 'dbo')
DROP TABLE dbo.months
CREATE TABLE dbo.months
(
[fldmonth] VARCHAR(20) NOT NULL
)
ON [PRIMARY]
GO

USE testingdb
DECLARE @maxRandomValue TINYINT,
@minRandomValue TINYINT,
@cnt INT
SET @maxRandomValue = 12
SET @minRandomValue = 1
SET @cnt = 1000000
WHILE @cnt > 0
BEGIN
SET @cnt = @cnt - 1

INSERT INTO dbo.monthsno
([fldmonth])
SELECT CAST(CAST(( ( @maxRandomValue ) - @minRandomValue ) * Rand() + @minRandomValue AS TINYINT) AS VARCHAR)
END
GO

USE testingdb
INSERT INTO dbo.months
([fldmonth])
SELECT Datename(MONTH, Dateadd(MONTH, [fldmonth] - 1, 0)) AS monthname
FROM dbo.monthsno
GO

DROP TABLE dbo.monthsno



Now, to test the time I ran each query surrounded by: -
DECLARE @time datetime
SET @time=getdate()
/*CODE*/
SELECT datediff(ms,@time,getdate()) as "Time Taken"



Firstly: -
SELECT MONTH([fldmonth] + ' 1 2010')
FROM dbo.months



After 5 attempts I got - 4513, 4453, 4453, 4606 and 4426, so roughly 4.5 seconds on 1 million rows.

Secondly: -
SELECT ( CASE [fldmonth]
WHEN 'January' THEN 1
WHEN 'February' THEN 2
WHEN 'March' THEN 3
WHEN 'April' THEN 4
WHEN 'May' THEN 5
WHEN 'June' THEN 6
WHEN 'July' THEN 7
WHEN 'August' THEN 8
WHEN 'September' THEN 9
WHEN 'October' THEN 10
WHEN 'November' THEN 11
WHEN 'December' THEN 12
END )
FROM dbo.months



After 5 attempts I got - 4513, 4406, 4533, 4530 and 4516, so once again roughly 4.5 seconds on 1 million rows.

I think the problem was my use of the word "function" in my post. . . it wasn't the word I was after Smile


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218416 Visits: 41996
madhu.gut (5/6/2010)
Hi All
How should i convert the month name to month number like
'April' it should converted to 4
'January' it should converted to 1
in this how should i do this
can any one suggest the correct answer


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)




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search