在.net中,可以使用String.PadLeft函数右对齐字符串,在左边用指定的Unicode字符填充一达到指定的总长度。
      例如 在做自动编号这样使用
     int nextID=da.GetMaxNum()+1;
     this.txtCode.Text = nextID.ToString().PadLeft(6, '0');

今天客户想要在用户表里加个工号字段,这个字段是自动生成的,这个很容易实现。但是用户表里已经有300多条记录,需要编号,手工录入
显然太麻烦,实施人员也会感觉枯燥乏味,我就想在数据库里有写段脚本自动编号,发现数据里没提供类似于右对齐的函数。

于是左查右看,就写了这么个自定义函数

--功能:右对齐的字符,在左边用指定的字符填充以达到指定的总长度。
--@num:原始字符@paddingChar:填充字符 @totalWidth:结果字符串中的字符数
--create by guoliang 2009/06/12

create function PadLeft(@num varchar(16),@paddingChar char(1),@totalWidth int)

returns varchar(16) as

begin

declare @curStr varchar(16)

select @curStr = isnull(replicate(@paddingChar,@totalWidth - len(isnull(@num ,0))), '') + @num

return @curStr

end

--测试函数:

SELECT dbo.PadLeft('8', '0', 6)

----------------
000008

(所影响的行数为 1 行)

然后用游标一行一行取出来,修改工号字段就ok了.

实现SQL SERVER 下的PadLeft函数相关推荐

  1. SQL Server中的STRING_SPLIT函数

    This article will cover the STRING_SPLIT function in SQL Server including an overview and detailed u ...

  2. 学习SQL:SQL Server日期和时间函数

    So far, we haven't talked about SQL Server date and time functions. Today we'll change that. We'll t ...

  3. 什么是SQL Server GETDATE()函数?

    Hey, readers! In our previous article, we had understood the working of SQL Server DATEPART() functi ...

  4. coalesce函数_什么是SQL Server COALESCE()函数?

    coalesce函数 Hey, folks! In this article, we will be focusing on SQL Server COALESCE() function. 嘿伙计! ...

  5. percent sql_使用SQL Server中的PERCENT_RANK函数计算SQL百分位数

    percent sql This article explores the SQL Server PERCENT_RANK analytical function to calculate SQL P ...

  6. SQL Server多语句表值函数

    In this article, we will learn multi-statement table-valued functions (MSTVFs) basics and then we wi ...

  7. C# 学习笔记(19)操作SQL Server下

    C# 学习笔记(19)操作SQL Server下 ADO.net操作数据库 这应该是比较老的技术了,以后有空的话学学 Linq /// <summary> /// 数据库使用类 /// & ...

  8. 什么是SQL Server TRIM()函数?

    Hello, readers. In today's article, we will be focusing on SQL Server TRIM() function in detail. 您好, ...

  9. sql dateadd函数_什么是SQL Server DATEADD()函数?

    sql dateadd函数 Hey, folks! In this article, we will be focusing on SQL Server DATEADD() function in d ...

最新文章

  1. 一款嵌入式监听地图测试软件(开发)
  2. 深度学习总结:continuous actions和asyncronous advanteage actor-critic
  3. 大四中软实习笔记20130226
  4. SQL实战篇:SQL解决近X天的问题
  5. 【20171123】【GITC精华演讲】贝业新兄弟李济宏:如何做到企业信息化建设的加减乘除...
  6. asp.net 的page 基类页面 做一些判断 可以定义一个基类页面 继承Page类 然后重写OnPreLoad事件...
  7. flask中的session,render_template()第二和参数是字典
  8. leetcode513. 找树左下角的值(dfs)
  9. org.apache.ibatis.reflection.ReflectionException: There is no getter for property named XXX
  10. 【零基础学Java】—static关键字概述(十六)
  11. 成年人的世界,嘴上喊的都是主义,心里装的都是生意
  12. NodeJs之数据库异常处理
  13. mac推箱子c语言,c语言写的推箱子源码,非常适合新手学习
  14. 抖音便捷小空调特效 html+css+js
  15. Xcode8 及 iOS 10 的适配
  16. Linux修改配置文件(自动)
  17. ubuntu compiz 不能启动
  18. SPA项目开发之首页导航+左侧菜单
  19. 国腾GM7123C:功能RGB转VGA芯片方案简介
  20. minitab画出主效应图并画出等值线图

热门文章

  1. linux下collada-dom编译,COLLADA DOM Tutorial
  2. python中列表用某个数字出现的次数_Python实现统计给定列表中指定数字出现次数的方法...
  3. 计算机教学实验操作环境,一种基于虚拟机的个性化计算机实验教学环境.pdf
  4. html中可以添加几个类,从一个类中向一个HtmlHead对象添加多个元素
  5. 网站服务器被攻击的形式,服务器被攻击的不同表现类型以及应对策略
  6. php创蓝253四要素认证_PHP下基于创蓝253接口的短信发送
  7. 机器学习和图像资源整理
  8. 【杂谈】从学员到专栏作者、讲师,我在有三AI学习与名利双收的故事
  9. 全球及中国农业保险市场营销状况与运营价值分析报告2022版
  10. how to fix error 25007 : loadlibraryshim()