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

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

2012-01-30 
怎么样用函数或其它办法实现以下功能?createtabletest(ttvarchar(5))insertintotestselect08:00unionallse

怎么样用函数或其它办法实现以下功能?
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
*/

热点排行
Bad Request.