ps:随笔写的在sql server中要用到的 (重复、替换、截取、去空格、去小数点后的位数)
/*---------------------------重复--------------------------------*/
--重复字符串 返回【abc#abc#abc#】
select replicate('abc#', 3);
/*---------------------------替换--------------------------------*/
--替换字符串 将e替换成e 返回【abcedef】
--replace('字符串', '替换前的文字', '替换后的文字')
select replace('abcedef', 'e', 'e');
--指定位置替换字符串 返回【heabcworld】
--stuff('字符串', 从哪里开始替换, 替换几位, '要替换的字符')
select stuff('hello world', 3, 4, 'abc');
/*----------------------------截取--------------------------------*/
--截取字符串 返回【a ,ab ,wrold】
--substring('字符串', 从哪里开始截取, 截取几位)
select substring('abc', 1, 1), substring('abc', 1, 2), substring('hello wrold', 7, 5);
--取左边字符串 返回【left,leftstr】
--left('字符串', 从左边开始取几位)
select left('leftstring', 4);
select left('leftstring', 7);
--取右边字符串 返回【string,ing】
--right('字符串', 从右边开始取几位)
select right('leftstring', 6);
select right('leftstring', 3);
/*---------------------------去空格----------------------------------*/
--去掉左边空格
select ltrim(' abc'), ltrim('# abc#'), ltrim(' abc');
--去掉右边空格
select rtrim(' abc '), rtrim('# abc# '), rtrim('abc');
/*-------------------------去小数点后的位数----------------------------*/
--用函数round(数值,s) ,其中s 表示小数位数
select round(4.994,2) --返回4.990
--用函数cast(数值as numeric(n,s)),其中n表示有效数字位数,s表示小数位数
select cast(4.994 as numeric(10,2))--搜索返回4.99
--用函数convert(numeric(n,s),数值),其中n表示有效数字位数,s表示小数位数
select convert(numeric(10,2),4.9852222)-- 返回4.99