读书人

如何样用函数或其它办法实现以下功能

发布时间: 2012-01-30 21:15:58 作者: rapoo

怎么样用函数或其它办法实现以下功能?
create table test(tt varchar(5))

insert into test
select '08:00 '
union all
select '08:00 '
union all
select '08:00 '
union all
select '08:00 '


现在要实现的是
tt aa (效果)
08:00 08:01
08:00 08:02
08:00 08:10
08:00 08:22

要求就是后列要随机生成的。

以前我是用游标,但速度不够快。


[解决办法]
alter table test add id int identity(1,1)
go
select tt,left(tt,3)+right( '00 '+rtrim(cast(rand()*(60-id) as int)),2) as aa from test
[解决办法]
alter table test add id int identity(1,1)

SELECT TT,AA=RIGHT(CONVERT(CHAR(16),DATEADD ( minute ,CAST( rand()*60+ID AS INT), TT ),21),5) FROM TEST
[解决办法]
--或者也可

create table test(tt varchar(5))

insert into test
select '08:00 '
union all
select '08:00 '
union all
select '08:00 '
union all
select '08:00 '
GO
Select
tt,
Left(tt, 3) + Right(100 + Cast(Rand(CheckSUM(NewID())) * 59 As Int), 2) As aa
From
test
GO
Drop Table test
--Result
/*
ttaa
08:0008:32
08:0008:05
08:0008:21
08:0008:26
*/

读书人网 >SQL Server

热点推荐