当前位置: 代码迷 >> Sql Server >> 整数转化月份的有关问题
  详细解决方案

整数转化月份的有关问题

热度:38   发布时间:2016-04-27 19:51:19.0
整数转化月份的问题
在存储过程中如何把1,2,3,4,,,,12这样的整数转化为英语月份的简写?
比如:1转化为Jan,4转化为Apr,9转化为Sept等等

------解决方案--------------------
case ... when ... else ... end
------解决方案--------------------
declare @month int
case @month when 1 then 'Jan ' when 2 then .....
--12个不算多
------解决方案--------------------
create table tb (tb_month int)
insert into tb values(1)
insert into tb values(2)
insert into tb values(3)
insert into tb values(4)
insert into tb values(5)
insert into tb values(6)
insert into tb values(7)
insert into tb values(8)
insert into tb values(9)
insert into tb values(10)
insert into tb values(11)
insert into tb values(12)

select tb_month ,
eng_month = case tb_month
when 1 then 'Jan '
when 2 then 'Feb '
when 3 then 'Mar '
when 4 then 'Apr '
when 5 then 'May '
when 6 then 'Jun '
when 7 then 'Jul '
when 8 then 'Aug '
when 9 then 'Sept '
when 10 then 'Oct '
when 11 then 'Nov '
when 12 then 'Dec '
end
from tb

drop table tb

/*
tb_month eng_month
----------- ---------
1 Jan
2 Feb
3 Mar
4 Apr
5 May
6 Jun
7 Jul
8 Aug
9 Sept
10 Oct
11 Nov
12 Dec

(所影响的行数为 12 行)
*/
  相关解决方案