Converting integer dates into string values in TSQL
Declare @Year INT = 2020
Declare @Quarter TINYINT = 1
How to convert into string to show as 2020 Q1
我会使用CONCAT
因为它会为您处理数据类型转换。
SELECT CONCAT(@Year, ' Q', @Quarter) as QtrString
Eric Brandt has a great option.
Adding Another option, if you want to do the data transformations yourself, use:
Declare @Year INT = 2020
Declare @Quarter TINYINT = 1
Select CONVERT(varchar(4), @Year) + ' Q' + CONVERT(varchar(4),@Quarter);
use datefromparts
declare @Year int=2020
declare @Quarter int=1;
select datefromparts(@Year, case when @Quarter=1 then 1 when @Quarter=2 then 4 when @Quarter=3 then 7 when @Quarter=4 then 10 end, 1)
output
2020-01-01
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.