日期:2014-05-18  浏览次数:20413 次

如何把其中的STRING换成随即的数值,而且随机数能控制一个范围
例如 

COL001
------------------------------
0012008010106003707887204090
0012008010106481205201966090
0012008010107464207861626090
0012008010107474403140821090
0012008010106003707887204090
0012008010106481205201966090
0012008010107464207861626090
0012008010107474403140821090
0012008010107474614168049090


我要把 substring(COL001,12,4) 里面的4个字符---在里面的意义就是 代表时间
换成在 0820~0835之间的随即数字。

可以有部分重复,但是要随机,不知道SQL达人们有什么好的见解



------解决方案--------------------
随即, 可以用算法实现不是随机么?就是由规律的换成在 0820~0835之间的数字。
------解决方案--------------------
SQL code
select  '0'+ltrim(cast(rand()*16 as int)+820)

------解决方案--------------------
select coloo1 from 表 where substring(coloo1,12,4) >='0820' and substring(coloo1,12,4) <='0835'
------解决方案--------------------
SQL code
create table tb(COL001 varchar(64)) 
insert tb select '0012008010106003707887204090' 
union all select '0012008010106481205201966090' 
union all select '0012008010107464207861626090' 
union all select '0012008010107474403140821090' 
union all select '0012008010106003707887204090' 
union all select '0012008010106481205201966090' 
union all select '0012008010107464207861626090' 
union all select '0012008010107474403140821090' 
union all select '0012008010107474614168049090' 

update tb 
set col001=stuff(COL001, 12, 4, right(10000+abs(checksum(newid()))%16+820, 4))

select * from tb
/*
COL001
----------------------------
0012008010108203707887204090
0012008010108311205201966090
0012008010108244207861626090
0012008010108214403140821090
0012008010108203707887204090
0012008010108351205201966090
0012008010108344207861626090
0012008010108354403140821090
0012008010108264614168049090

(9 row(s) affected)
*/

drop table tb