Link to home
Start Free TrialLog in
Avatar of kowsika devi
kowsika devi

asked on

convert month name to month number in sql server 2012

convert month name to month number in sql server 2012
Eg : Aug means current yearand month (201708)
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Where did you store the month names?
Avatar of kowsika devi
kowsika devi

ASKER

its in my table already i saved i need to change aug as 201708 formats
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks Vitor Montalvão :-)
another way to do that

declare @yourTable table
(
	YourMonthName varchar(3)
)
insert into @yourTable
values
('Jan'),
('Feb'),
('Mar'),
('Apr'),
('May'),
('Jun'),
('Jul'),
('Aug'),
('Sep'),
('Oct'),
('Nov'),
('Dec')

select Convert(varchar(6), CAST(CONCAT('1', YourMonthName, Year(getdate())) as datetime ), 112) YearMonth
from @yourTable

Open in new window

one more  :)
declare @yourTable table
(
	YourMonthName varchar(3)
)
insert into @yourTable
values
('Jan'),
('Feb'),
('Mar'),
('Apr'),
('May'),
('Jun'),
('Jul'),
('Aug'),
('Sep'),
('Oct'),
('Nov'),
('Dec')


Declare @sYear varchar (4)='2017'
select YourMonthName,CONVERT(varchar(6), convert(DATETIME,YourMonthName+' 1 '+@sYear+' 12:00AM'),112)
from @yourTable

Open in new window

yet another one

declare @mon as varchar(3)='Aug';
select 1+CharIndex(@mon,'JanFebMarAprMayJunJlyAugSepOctNovDec')/3

8

Open in new window

ID: 42253666 : proposed solution is not the only one...
and being the first is not enough to get whole share :)
also, it always return 2017XX...

ID: 42253776
ID: 42254508
ID: 42254672

all provide some solution to the question, and points should be shared...