SQL实现数字转固定长度字符串,不足长度补0,如1转换为001

  Replace函数,Replace (str1, str2, str3),意思是,在字符串str1中,当str2出现,则用str3代替

而要实现SQL数据转固定长度字符串,且不足长度补0,如1转为001,则可以如下:

replace(str(id,3),' ','0'),即先将字段id转换为3位的str类型字符,然后将其中的 ' ' (空字符) 转换为0

例:select replace(str(LineCode,4),' ','0') from [tb_HR_SM_ApplyPaySub] where MainId=8893

例:update tb_HR_SM_ApplyPaySub set LineCode=replace(str(LineCode,4),' ','0') where MainId=8893

猜你喜欢

转载自blog.csdn.net/Annn_kk/article/details/81180039
今日推荐