SQL get timestamp serial number

Serial number generation rules:

1: The total length of the serial number is 22 digits

2: The serial number is divided into three parts: header (2 digits) + timestamp (YYYYMMDDHHmmSSsss a total of 17 digits) + random code (3 digits)

     Example serial number: SN20150812102400111234

 

1
2
3
4
5
6
7
8
9
10
11
--获取时间戳
select  convert ( varchar , replace ( replace ( replace ( replace ( convert ( varchar ,getdate(),121), '-' , '' ), ':' , '' ), ' ' , '' ), '.' , '' ))
--结果:20150703114447613
 
--获取随机码
select  substring ( convert ( varchar ,rand()),3,3)
--结果:813
 
--获取完整的流水号
SELECT  'SN' + convert ( varchar , replace ( replace ( replace ( replace ( convert ( varchar ,getdate(),121), '-' , '' ), ':' , '' ), ' ' , '' ), '.' , '' ))+ substring ( convert ( varchar ,rand()),3,3)
--结果:SN20150703114447613813

Reprinted from: https://www.cnblogs.com/DBArtist/p/SerialNumber.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324817306&siteId=291194637