日期:2014-05-17 浏览次数:20640 次
;with cte(站号 ,年, 月, 日, 值) as
(
select 'jji',2001,1,1,45
union all select 'kij',2001,1,2,89
)
select 站号,CAST(年 as varchar)+right('0'+CAST(月 as varchar),2)+right('0'+CAST(日 as varchar),2) as 日期,值
from cte
/*
站号 日期 值
jji 20010101 45
kij 20010102 89
*/
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-30 12:53:29
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([站号] varchar(3),[年] int,[月] int,[日] int,[值] int)
insert [huang]
select 'jji',2001,1,1,45 union all
select 'kij',2001,1,2,89
--------------开始查询--------------------------
select [站号],CAST([年] AS CHAR(4))+RIGHT('0'+CAST([月] AS varchar(2)),2)+RIGHT('0'+CAST([日] AS varchar(2)),2) AS [日期],[值]
from [huang]
----------------结果----------------------------
/*
站号 日期 值
---- ---------- -----------
jji 20010101 45
kij 20010102 89
*/
;with t(站号 ,年, 月, 日, 值)
as
(
select 'jji',2001,1,1,45 union all
select 'kij',2001,1,2,89
)
select 站号,
replace(convert(varchar(10),cast(CAST(年 as varchar)+'-'+
CAST(月 as varchar)+'-'+
CAST(日 as