SQL操作字符串及数字汇总

it2022-05-05  97

代码  1  --=========== 字符串使用汇总 ================  2   3  -- 将字符串中从某个字符开始截取一段字符,然后将另外一个字符串插入此处  4  select stuff( ' hello,world! ' , 4 , 4 , ' **** ' )    -- 返回值hel **** orld !  5   6  -- 返回从指定位置开始指定长度的字符串  7  select substring( ' Hello,World! ' , 2 , 10 )    -- 返回值ello,World  8   9  -- 将字符串中某段字符替换为指定的字符串 10  select replace( ' hello,world! ' , ' ll ' , ' aa ' -- 返回值heaao,world ! 11  12  -- 去除字符串中左边的空格 13  select ltrim( '    hello,world! ' )     -- 返回值hello,world ! 14  15  -- 去除字符串中左边的空格 16  select ltrim( ' hello,world!    ' )     -- 返回值hello,world ! 17  18  -- 去除字符串中左边和右边的空格 19  select ltrim( '     hello,world!    ' )    -- 返回值hello,world ! 20  21  -- 将NULL值替换为指定字符 22  select isnull( ' a ' , null )      -- 返回值a 23  24  -- 转换数据类型 25  select cast( ' 2007-10-11 '   as  datetime)    -- 返回值2007 - 10 - 11   00 : 00 : 00.000 26  select convert(datetime, ' 2007-10-11 ' )    -- 返回值2007 - 10 - 11   00 : 00 : 00.000 27  28  -- 获取字符串长度 29  select len( ' hello,world! ' )     -- 返回值12 30  31  -- 获取字符串的前3个字符 32  select left( ' hello,world! ' , 3 )     -- 返回值hel 33  34  -- 获取字符串的后3个字符 35  select right( ' hello,world! ' , 3 )     -- 返回值ld ! 36  37  -- 去除字符串的前3个字符 38  select right( ' hello,world! ' ,(len( ' hello,world! ' ) - 3 ))  -- 返回值lo,world ! 39  40  -- 去除字符串的后3个字符 41  select left( ' hello,world! ' ,(len( ' hello,world! ' ) - 3 ))  -- 返回值hello,wor 42  43  -- 获取在该字符串中某字符串的位置(返回数字)  44  select charindex( ' e ' , ' hello,world! ' )    -- 返回值2 45  46  -- 返回从第2个字符开始前4个字符 47  select left(right( ' [哈哈哈哈]aaa ' ,len( ' [哈哈哈哈]aaa ' ) - 1 ), 4 -- 返回值哈哈哈哈 48  49  -- 返回字符的小写形式 50  select lower( ' HELLO,WORLD! ' )     -- 返回值hello,world ! 51  52  -- 返回字符的大写形式 53  select UPPER( ' hello,world! ' )     -- 返回值HELLO,WORLD ! 54  55  -- 用第三个表达式替换第一个字符串表达式中出现的所有第二个指定字符串表达式的匹配项 56  (如果其中有一个输入参数属于 nvarchar 数据类型,则返回 nvarchar;否则返回 varchar。如果任何一个参数为 NULL,则返回 NULL。) 57  SELECT REPLACE( ' Hello,World! ' , ' l ' , ' a ' )    -- 返回值Heaao,Worad ! 58  SELECT REPLACE( ' Hello,World! ' , ' l ' , '' )    -- 返回值Heo,Word ! 59  SELECT REPLACE( ' Hello,World! ' , ' l ' , null )    -- 返回值NULL 60  61  -- 以右边参数数值次数复制字符表达式 62  select REPLICATE( ' Hello,World! ' , 4 )    -- 返回值Hello,World ! Hello,World ! Hello,World ! Hello,World ! 63  64  -- 返回反转后的字符串 65  select REVERSE( ' Hello,World! ' )     -- 返回值 ! dlroW,olleH 66  67  -- 使用DIFFERENCE时,两个字符串发音越相似(仅限于英文字符),返回值越大(返回值在0 - 4之间) 68  DIFFERENCE( ' sun ' , ' san ' )     -- 返回值4 69  DIFFERENCE( ' sun ' , ' safdsdf ' )    -- 返回值3 70  DIFFERENCE( ' sun ' , ' dgffgfdg ' )    -- 返回值0 71  72  -- 将带小数点的数字类型转换为可设定长度可设定小数位的四舍五入后的字符串 73  SELECT STR( 123.34584 7 3 )    -- 返回值123. 346 74  -- 当设定长度值小于整数部位长度时,字符串将返回设定长度个 * 75  SELECT STR( 123333.34584 5 4 )    -- 返回值 ***** 76  77  --=================================================================================== 78  79  --===================================== 数字操作汇总 ================================== 80  81  -- 返回指定数字的最大整数 82  select floor( 123456.1234 )    -- 返回值123456 83  84  -- 返回不带小数部分并且不小于其参数的值的最小数字。如果参数是一个空序列,则返回空序列 85  select ceiling( 123.010 )     -- 返回124 86  select ceiling( null )     -- 返回NULL 87  88  -- 返回四舍五入后的最接近该数值的数值 89  select round( 126.018 , 2 )     -- 返回126. 12 90  91  -- 返回一个0 - 1之间的FLoat类型的随机数 92  select rand()      -- 返回0. 94170703697981 93  94  -- 返回圆周率PI的值 95  SELECT PI()      -- 返回3. 14159265358979 96  97  98  文章出处:DIY部落(http: // www.diybl.com/course/7_databases/database_other/20090213/155355.html)

 

转载于:https://www.cnblogs.com/angleSJW/archive/2009/12/17/1626080.html

相关资源:数据结构—成绩单生成器

最新回复(0)