我在sql中有两列
year month
2016 4
2014 5
我现在要做的是将这两列组合在一起并得到句号
输出
year month result
2016 4 201604
2014 5 201405
有办法做到这一点吗?
答案 0 :(得分:8)
如果列数据类型是整数,请执行
select year * 100 + month from tablename
答案 1 :(得分:3)
首先,您需要CAST
他们到VARCHAR
以允许连接。然后使用RIGHT
month
来填充值0
:
WITH Tbl AS(
SELECT 2016 AS [year], 4 AS [month] UNION ALL
SELECT 2014, 5
)
SELECT *,
CAST([year] AS VARCHAR(4)) + RIGHT('0' + CAST([month] AS VARCHAR(2)), 2)
FROM Tbl
答案 2 :(得分:1)
假设两列都是数字类型:
SELECT year,
month,
CAST(year as char(4)) + RIGHT('00' + CAST(month as varchar(2)), 2) as result
FROM YourTable