问题描述
字段类型
------------------------
amonth int
ayear int
acombine varchar(8)
amonth | ayear | acombine
--------- | ---------------------
1 | 2016 | 201601
2 | 2016 | 201602
3 | 2016 | 201603
11 | 2016 | 201611
12 | 2016 | 201612
你如何连接'amonth'和'ayear'来获得 acombine ?
请协助
谢谢
我尝试了什么:
已经从互联网上检查过了
Fields type
------------------------
amonth int
ayear int
acombine varchar(8)
amonth | ayear | acombine
---------|---------------------
1 | 2016 | 201601
2 | 2016 | 201602
3 | 2016 | 201603
11 | 2016 | 201611
12 | 2016 | 201612
How do you concatenate 'amonth' and 'ayear' to get acombine?
Please assist
Thanks
What I have tried:
HAVE CHECKED ON IT FROM THE INTERNET
推荐答案
--create a temporary table
create table #tmp
(
amonth int ,
ayear int ,
acombine varchar(8)
)
--insert dummy data
insert into #tmp values (1 ,2016 , '201601')
insert into #tmp values (2 , 2016 ,'201602')
insert into #tmp values (3 , 2016 , '201603')
insert into #tmp values (11 ,2016 , '201611')
insert into #tmp values (12 , 2016 , '201612')
--to get exact 'acombine' value try this
select CONVERT(NVARCHAR, ayear)+ Right('00' + Convert(varchar(10),amonth), 2) as acombine from #tmp
OP will be
201601
201602
201603
201611
201612
谢谢
Thanks
这篇关于需要连接年份和月份以维持2位数月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!