简体   繁体   中英

Is there a way I can concatenate two columns with different functions?

Is there a way I can concatenate two columns with different functions?

From the example, can I make it look like Q3 2015

在此处输入图片说明

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, 

Try this

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])) 

If you want to get Q YYYY , then the easiest way is to use datename() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM