如何将单个列分成2列 [英] How to divide a single column into 2 columns
问题描述
我有一个Table TableFirst
有单列月份
TableFirst
月
-----
Jan
2月
Mar
4月
可能
Jun
Jul
8月
Sep
十月
十一月
十二月
所需的O / P: -
Month1 Month2
------ ------
Jan Jul
2月8月
3月9月
4月10月
5月11月
Jun Dec
请帮助我...
你的问题不是关于拆分列,而是关于生成3 - 当前月份距离6个月的月份名称月份列中的第n个值。
首先,需要一个月的字符串,例如'Jan',把它变成月号=> 1并向其添加6 => 7喜欢这样:
选择(DATEPART(MM,'Jan'+'01 2008')+ 6)
接下来,将月份数转换为某个旧日期=> 2008年7月1日00:00:00 + 0000,如下:
选择DATEADD(月,(DATEPART(MM,'Jan'+'01 2008')+ 6 ) - 1,CAST('2008-01-01'AS datetime))
最后,我们只对该日期的3个月的新月感兴趣,所以这样做:
SELECT转换(char(3),DATEADD(MONTH,(DATEPART(MM,'Jan'+'01 2008')+ 6) - 1,CAST ('2008-01-01'AS datetime)))
理解这一点,然后你应该能够使用简单的选择查询自己解决问题的解决方案。 / BLOCKQUOTE>
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...解决方案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.
这篇关于如何将单个列分成2列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!