日期:2014-05-17 浏览次数:20477 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2014-01-17 17:23:30
-- 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: )
--
----------------------------------------------------------------
--> 测试数据:test
if object_id('test') is not null drop table test
go
create table test([id] INT IDENTITY(1,1),[letter] varchar(20))
insert test(letter)
select 'A'
UNION ALL
SELECT 'B'
UNION ALL
SELECT 'C'
UNION ALL
SELECT 'D'
--------------开始查询--------------------------
select *,CONVERT(CHAR(4),DATEADD(YEAR,id,'2009-01-01'),120)[Year]
from test
----------------结果----------------------------
/*
id letter Year
----------- -------------------- ----
1 A 2010
2 B 2011
3 C 2012
4 D 2013
*/
;with t
as
(
select 2010 year,'A' ap,0 number union all
select 2011 year,'B' ap,1 number union all
select 2012 year,'C' ap,2 number union all
select 2013 year,'D' ap,3 number union all
select 2014 year,'E' ap,4 number union all
selec