sql 生成流水號

2021-08-31 14:58:23 字數 566 閱讀 3246

mysql生成流水號: select lpad(ifnull(max(substring(business_order_code,3,4))+1,1),4,0) as waternumber from bh_customer_info

business_order_code的值為前面兩位英文,後面四位數字,例如wh0001,lb0510

bh_customer_info這是表名。

substring(business_order_code,3,4) 獲得的值為0001,0510

max(substring(business_order_code,3,4))+1獲得該列中最大值並加1

ifnull(max(substring(business_order_code,3,4))+1,1)如果該列中的值為null,則賦值為1,這種情況只在資料庫表中無資料的時候才起到作用。

lpad(ifnull(max(substring(business_order_code,3,4))+1,1),4,0)該函式的意思是,總共四位數,如果位數不足,則用0填充。

oracle判斷列值為空時用nvl代替mysql的ifnull

SQL生成流水號

經過了幾次的測試終於成功了 declare year int,month int,day int,temp no varchar 12 needno varchar 4 no varchar 20 number varchar 50 randno varchar 50 nu varchar 10 s...

SQL自動生成流水號

select convert char 6 getdate 12 下面的 生成長度為8的編號,編號以bh開頭,其餘6位為流水號。得到新編號的函式 create function f nextbh returns char 8 asbegin 從表裡得到最大值加個1000001就增乙個1 return...

SQL流水號生成語句

table num createdate 空 20090901 空 20090901 空 20090902 空 20090902 空 20090903 空 20090903 現在要通過createdate來產生乙個流水號 4位 按當天來生成 換天要重新生成 即得到結果如下 table num cre...