有没有办法可以连接两个具有不同功能的列?

时间:2015-11-18 13:55:37

标签: sql sql-server

有没有办法可以连接两个具有不同功能的列?

从示例中,我可以看起来像2015年第3季度

enter image description here

SELECT convert(varchar(20), [Evaluation Date],(101)),
       month([Evaluation Date]), year([Evaluation Date]), 
       case when month([Evaluation Date]) in ('1','2','3') then 'Q1'
            when month([Evaluation Date]) in ('4','5','6') then 'Q2'
            when month([Evaluation Date]) in ('7','8','9') then 'Q3'
            when month([Evaluation Date]) in ('10','11','12') then 'Q4'
       end as [Month],
       year([Evaluation Date]) as [Year], 
       DATEADD(MONTH, DATEDIFF(MONTH, 0, [Evaluation Date]), 0) AS MonthYear, 

2 个答案:

答案 0 :(得分:1)

试试这个

SELECT convert(varchar(20), [Evaluation Date],(101)),
   month([Evaluation Date]), year([Evaluation Date]), 
   case when month([Evaluation Date]) in ('1','2','3') then 'Q1'
        when month([Evaluation Date]) in ('4','5','6') then 'Q2'
        when month([Evaluation Date]) in ('7','8','9') then 'Q3'
        when month([Evaluation Date]) in ('10','11','12') then 'Q4'
   end + ' ' + CONVERT(nvarchar, year([Evaluation Date])) 

答案 1 :(得分:1)

如果您想获得Q YYYY,那么最简单的方法就是使用datename()

select ('Q' + datename(quarter, [Evaluation Date]) + ' ' +
        datename(year, [Evaluation Date])
       ) as QYYYY