首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问一SQL

2012-03-01 
请教一SQLnamebdateedatetom2007-7-12007-7-4想得到tom2007-7-1tom2007-7-2tom2007-7-3tom2007-7-4[解决办

请教一SQL
name             bdate             edate
tom             2007-7-1         2007-7-4

想得到
tom         2007-7-1
tom         2007-7-2
tom         2007-7-3
tom         2007-7-4

[解决办法]
Create Table TEST
(name Varchar(10),
bdate DateTime,
edate DateTime)
Insert TEST Select 'tom ', '2007-7-1 ', '2007-7-4 '
GO
Select TOP 1000 ID = Identity(Int, 0, 1) Into #T From SysColumns A, SysObjects

Select
A.name,
Convert(Varchar(10), DateAdd(dd, B.ID, A.bdate), 120) As [date]
From
TEST A
Inner Join
#T B
On DateAdd(dd, B.ID, A.bdate) <= A.edate

Drop Table #T
GO
Drop Table TEST
--REsult
/*
namedate
tom2007-07-01
tom2007-07-02
tom2007-07-03
tom2007-07-04
*/
[解决办法]
declare @a table(name varchar(10), bdate smalldatetime, edate smalldatetime)
insert @a select 'tom ', '2007-7-1 ', '2007-7-4 '

select top 31 id=identity(int,0,1) into # from syscolumns

select * from
(select name,x=case when dateadd(day,id,bdate) <=edate then convert(varchar(10),dateadd(day,id,bdate),120) else ' ' end from @a a,# b) aa
where x <> ' '
drop table #

热点排行