在sql server中,如何将整数转换为日期和时间,例如:
declare @constant int =92011 or 102011
如何将其转换为日期格式,如“2011年9月”或“2011年10月”。
答案 0 :(得分:0)
declare @c int
set @c = 102011
;with M(Num, Name) as
(
select 1, 'January' union all
select 2, 'February' union all
select 3, 'March' union all
select 4, 'April' union all
select 5, 'May' union all
select 6, 'June' union all
select 7, 'Juli' union all
select 8, 'August' union all
select 9, 'September' union all
select 10, 'October' union all
select 11, 'Novmber' union all
select 12, 'December'
)
select M.Name+'-'+right(@c, 4)
from M
where M.Num = left(@c, len(@c)-4)
受到DATENAME提供的答案中使用Upendra Chaudhari的启发。
select datename(month, dateadd(month, left(@c, len(@c)-4) - 1, 0))+'-'+right(@c, 4)
答案 1 :(得分:0)
试试这个:
declare @c int
set @c = 92011
SELECT DATENAME(MONTH,CAST(RIGHT(@c,4) + '-' + LEFT(@C, LEN(@C)-4) + '-01' AS DATE)) + '-' + RIGHT(@c,4)
或 以下案例:
declare @c int
set @c = 92011
select CASE WHEN LEFT(@C, LEN(@C)-4) = 1 THEN 'January'
WHEN LEFT(@C, LEN(@C)-4) = 2 THEN 'February'
WHEN LEFT(@C, LEN(@C)-4) = 3 THEN 'March'
WHEN LEFT(@C, LEN(@C)-4) = 4 THEN 'April'
WHEN LEFT(@C, LEN(@C)-4) = 5 THEN 'May'
WHEN LEFT(@C, LEN(@C)-4) = 6 THEN 'June'
WHEN LEFT(@C, LEN(@C)-4) = 7 THEN 'July'
WHEN LEFT(@C, LEN(@C)-4) = 8 THEN 'August'
WHEN LEFT(@C, LEN(@C)-4) = 09 THEN 'September'
WHEN LEFT(@C, LEN(@C)-4) = 10 THEN 'October'
WHEN LEFT(@C, LEN(@C)-4) = 11 THEN 'November'
WHEN LEFT(@C, LEN(@C)-4) = 12 THEN 'December'
ELSE '' END + '-' + RIGHT(@c,4)