Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have a Table TableFirst
which has single column month

TableFirst

Month
-----
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec

Desired O/P:-

Month1 Month2
------ ------
Jan Jul
Feb Aug
Mar Sep
Apr Oct
May Nov
Jun Dec

Kindly help me...
Posted
Comments
Andrius Leonavicius 4-Jul-14 17:12pm    
Hi,

If it's only 12 values, why can't you just do it manually? Where is the problem?
StackQ 4-Jul-14 23:55pm    
may be more than 12 values, its not fixed, just an example
Andrius Leonavicius 5-Jul-14 11:07am    
What is the general rule for putting month values into two columns (in case of more than 12 values)?

1 solution

Your question is not about splitting column, rather it is about generating the 3-letter month name that are 6 months away from the current month values in the month column.
first, take a month string, e.g. 'Jan', turn it into month number => 1 and add 6 to it => 7 like this:
select (DATEPART(MM, 'Jan' + ' 01 2008') + 6)

next, convert the month number into some old date => July, 01 2008 00:00:00+0000, like this:
select DATEADD(MONTH, (DATEPART(MM, 'Jan' + ' 01 2008') + 6) - 1, CAST('2008-01-01' AS datetime))

Finally, we are only interested in the new 3-letter month of the date, so do this:
SELECT Convert(char(3), DATEADD(MONTH, (DATEPART(MM, 'Jan' + ' 01 2008') + 6) - 1, CAST('2008-01-01' AS datetime)))

Understand this then you should be able to work out the solution to your question yourself using simple select query.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900