需要连接年份和月份以维持2位数月份 [英] Need to concatenate years and months to maintain 2 digits months

查看:92
本文介绍了需要连接年份和月份以维持2位数月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字段类型

------------------------

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

推荐答案

你必须用前导零来格式化数字

试试这个

You have toi format the numbers by leading zero
try this
--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位数月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆