为了账号安全,请及时绑定邮箱和手机立即绑定

SQL SERVER 函数收集

标签:
SQL Server C#

--字符串函数
--ascii(字符串表达式),返回字符串中最左侧的字符的ascii码
select ascii('a')
select ascii('ab')
--char(整数表达式),返回ascii码值对应的字符,码值范围0至255,超出这个范围则返回null
select char(120)
select char(256)
--charindex(字符串表达式1,字符串表达式2,[,整数表达式])返回子字符串1,在字符串2中的位置,若不存在返回null,另可以指定在字符串2中查找的起始位置
select charindex('ab','BCabTabD')
select charindex('ab','BCabTabD',4)
--difference(字符串表达式1,字符串表达式2) 返回一个0至4 之间的整数,指示两个字符表达式的之间的相似程度,0几乎不同或完全不同,4几乎相同或完全相同
select difference('','')
select difference('a','')
select difference('a','ab')
select difference('abcde','abcdef')
--left(字符串表达式,整数表达式) 返回字符串中从左边开始指定个数的字符
select left('abcdefg',2)
--right(字符串表达式,整数表达式) 返回字符串中从右边开始指定个数的字符
select right('abcdefg',2)
--len(字符串表达式) 返回指定字符串表达式的字符数,其中不包含尾随空格。
select len('abcdefg')
--lower(字符串表达式)返回大写字符数据转换为小写的字符表达式
select lower('ABCDEFG')
--upper(字符串表达式)返回小写字符数据转换为大写的字符表达式
select upper('abcdefg')
--1trim(字符串表达式) 返回删除了前导空格之后的字符表达式,注意 函数开头是字母L
select ltrim(' abc')
--1trim(字符串表达式) 返回删除了尾随空格之后的字符表达式,
select rtrim('abc   ')
select ltrim(rtrim('  abcd    '))
--patindex(字符串表达式1,字符串表达式2) 在字符串表达式1中可以使用通配符,
--此字符串的第一个字符和最后一个字符通常是%。%表示任意多个字符,_表示任意字符,
--返回字符串表达式2中字符串表达式1所指定模式第一次出现的起始位置。没有找到返回0 
select patindex('%ab%','123ab456')
select patindex('ab%','123ab456')
select patindex('___ab%','123ab456')
select patindex('___ab_','123ab456')
--reverse(字符串表达式) 返回指定字符串反转后的新字符串
select reverse('abcdefg')
--space(整数表达式)返回由指定数目的空格组成的字符串
select 'abc'+space(2)+'def'
--str(float型小数[,总长度[,小数点后保留的位数]])返回由数字转换成的字符串。
--返回字符数不到总长度的前面补空格,超过总长度的截断小数位。
--如果需要截断整数位则返回**。注意在截断时遵循四舍五入总长度。

--它包括小数点、符号、数字以及空格。默认值为10。小数点后最多保留16位。默认不保留小数点后面的数字
 select str(123.456)
 select str(123.456,3)
 select str(123.456,7,2)
 select str(123.456,5,3)
 select str(123.456,16,5)
 select len(str(123.456,16))
 --stuff(字符串表达式1,开始位置,长度,字符串表达式2) 在字符串表达式1中在指定的开始位置删除指定长度的字符,并在指定的开始位置出插入字符串表达式2,返回新字符串
 select stuff('abcdefg',2,2,'AB')
 --substring(字符串表达式,开始位置,长度) 截取指定位置,和长度的子字符串并返回
 select substring('abcdefg',2,3)
 --replace(字符串表达式1,字符串表达式2,字符串表达式,3)用串3代替串1中出现的所有串2的匹配项,并返回新串
 select replace('abcdefgabcdklmn','bc','@@')

 --日期和时间函数
 --dateadd(日期部分,数字,日期)
 select dateadd(year,30,getdate())
  select dateadd(yy,30,getdate())
 select dateadd(month,120,getdate())
  select dateadd(mm,120,getdate())
   select dateadd(day,120,getdate())
  select dateadd(dd,120,getdate())
   select dateadd(DD,120,getdate())
  select dateadd(hh,168,getdate())
    select dateadd(mi,120,getdate())
   select dateadd(MI,120,getdate())
     select dateadd(minute,120,getdate())
   select dateadd(HH,120,getdate())
  select dateadd(ss,120,getdate())
  --datediff(日期部分,开始部分,结束部分) 返回两个指定日期的指定日期部分的差的整数值,在计算是由结束日期剪去减去开始日期,
  select datediff(yy,'1949-10-01','2020-08-02')
    select datediff(mm,'1949-10-1',getdate())
   select datediff(dd,'1949-10-1',getdate())
     select datediff(dw,'1949-10-1',getdate())
   select datediff(dy,'1949-10-1',getdate())
--datename(日期部分, 日期) 返回表示指定日期的指定 日期部分 的字符串
select datename(yy,getdate())
select datename(mm,getdate())
select datename(dd,getdate())
select len(datename(yy,getdate()))
select len(getdate())
--datepart(日期部分,日期)返回表示指定日期的指定 日期部分 的整数
select datepart(yy,getdate())
select datepart(mm,getdate())
select datepart(dd,getdate())
--getdate() 返回当前日期和时间
select getdate()
--day(日期) 返回指定日期的天的部分,整数
select day(getdate())
select datepart(dd,getdate())
--month(日期)返回指定日期的月的部分,整数,相当于datepart(mm,getdate())
select month(getdate())
--year(日期)返回指定日期的年的部分,整数,相当于datepart(yy,getdate())
select year(getdate())
--getutcdate() 返回格林尼治时间,和北京时间晚8个小时
select getutcdate()
select getdate()


--日期部分(指定要返回新值的日期的组成部分。下表列出了
-- Microsoft SQL Server 2008 可识别的日期部分及其缩写。)

--日期部分  含义  缩写
--year      年    yy, yyyy 
--quarter   季    qq, q 
--month     月    mm, m 
--dayofyear 天    dy, y 
--day       天    dd, d 

--week      星期  wk, ww 
--weekday   天    dw, w 

--hour      小时  hh 
--minute    分钟  mi, n 
--second    秒    ss, s 
--millisecond毫秒 ms

--数学函数
--abs(数值表达式) 返回式子的绝对值
select abs(-23.4)
select abs(4-5)
select abs(2-pi())
--pi() 无参返回pi值
select pi()
--cos(浮点表达式) 余玄值 注意浮点表达式应在取值范围内
select cos(pi()/3)
select cos(pi()/4)
--sin(浮点数表达式) 正玄值 注意浮点表达式应在取值范围内
select sin(pi()/3)
select sin(pi()/6)
select sin(pi())
--cot(浮点数表达式) 余切值 注意浮点表达式应在取值范围内
select cot(pi()/4)
select cot(pi()/3)
select cot(pi()/2)
--tan(浮点数表达式) 正切值 注意浮点表达式应在取值范围内
select tan(pi()/4)
select tan(pi()/3)
select tan(pi()/2)
--acos(浮点数表达式)  反余玄 注意浮点表达式应在取值范围内<=1
select acos(0.5)
select acos(0.707)
--asin(浮点数表达式) 反正玄 注意浮点表达式应在取值范围内小于等于1
select asin(0.2)
select asin(1)
--atan(浮点数表达式)  方余玄
select atan(1000000)
select atan(-1000000)
--degrees(数值表达式) 返回弧度相应的角度值
select hudu=degrees(pi())
select hudu=round(pi()/3.14159265*180,2,14)
--radians(数值表达式) 返回指定度数的弧度值
select dushu=radians(360) --返回6
select dushu=radians(360.0)--返回6.2831853...
--exp(浮点数表达式) 返回e的指定次幂,e=2.718281
select exp(4)
select exp(4.6)
--log(浮点表达式) 返回以e为底的对数,求自然对数。
select log(6)
select log(6.3)
--log10(浮点表达式) 返回以10为底的对数,
select log10(10000)
--ceiling(数值表达式) 向上取整
select ceiling(5.4)
select ceiling(-5.2)
--floor(数值表达式) 向下取整
select floor(5.4)
select floor(-5.2)
--sqrt(数值表达式) 返回数值表达式的平方根 注意表达式的取值范围大于等于零
select sqrt(2)
select sqrt(0)
--sign(数值表达式)表达式为正,返回1,为负,返回-1,为零,返回零
select sign(10)
select sign(-10)
select sign(0)
--rand()  返回0到1之间的随机浮点数,整数表达式为种子,使用相同的种子产生随机数相同。即使同一个种子值重复调用rand()会返回相同的结果,不指定种子则系统会随机生成种子
select rand(12)
select rand()
select rand()
--round(数值表达式[,长度[,操作方式]]) 返回一个数值,舍入到指定的长度。注意返回的数值和原数值的总位数没变化.
--长度:舍入精度。如果长度为正数,则将数值舍入到长度指定的小数位数。如果长
--度为负数,则将数值小数点左边部分舍入到长度指定的长度。注意如果长度为负数
----并且大于小数点前的数字个数,则将返回0。如果长度为负数并且等于小数点前的数字个数且操作方式为四舍五入时,
--最前面的一位小于5 返回 0,大于等于 5 导致错误出现,如果操作方法不是四舍五入时则不会出现错误,返回结果一律为
--0。 操作方式:默认为 0 遵循四舍五入,指定其他整数值则直接截断。
select round(12.1234,2)
select round(12.1234,2,-1)
select round(12.1234,-2)
select round(1122.1234,-3)
select round(12.1234,-3)
select round(12.1234,2)
select round(5236.333,-4,1)
--数据类型转换函数 convert(数据类型[(长度)],表达式[,样式]),
select convert(nvarchar, 123)
select N'年龄'+ convert(nvarchar, 23) --加n是为了是数据库识别Unicode 字符
select convert(nvarchar,getdate())
select convert(nvarchar(5),getdate())
select convert(nvarchar(10),getdate())
select convert(nvarchar(13),getdate())
select convert(nvarchar(16),getdate())
select convert(nvarchar(20),getdate(),120)
select convert(nvarchar,getdate(),101)--美国
select convert(nvarchar,getdate(),102)--英国
select convert(nvarchar,getdate(),103)--法国
select convert(nvarchar,getdate(),104)--德国
select convert(nvarchar,getdate(),120)--意大利

--cast(表达式 as 数据类型[,(长度)]) 将一种数据类型的表达式显式转换为另一种数据类型的表达式,
--日期类型数据转为字符数据类型的日期格式的部分样式表当两个不同数据类型的表达式用运算符组合后,
--数据类型优先级规则指定将优先级较低的数据类型优先转换为优先级较高的型。
--如果此转换不是所支持的隐式转换,则返回错误。
--当两个操作数表达式具有相同的数据类型时,运算的结果便为该数据类型。

select cast(123 as nvarchar)
select N'年龄'+ cast(23 as nvarchar(2))
select cast(23 as nvarchar(3))+ '年龄'

--如果需要把优先级高的数据类型转换优先级低的数据类型时需要使用数据类型转换函数进行显示转换。


--系统函数
--newid() 返回一个GUID值,即全局唯一标识符
select newid()
--isnumeric(任意表达式) 判断表达式是否为数值类型或是否转换成数值,返回1,0
select isnumeric(123)
select isnumeric('123adc')
select isnumeric('123')
--isnull(任意式1,任意式2) 返回具体的值。如果任意表达式1 不为 NULL,则返回它1的值;
--否则,在将任意表达式2 的类型转换为任意表达式 1 的类型(如果这两个类型不同)后,返回任意表式2 的值。
select isnull(null,N'没有值')
select isnull(N'@',N'值2')
select isnull(23,null)
--isdate(任意表达式) 判断是否为日期或可转换为日期,返回1,0
select isdate(getdate())
select isdate('08.03.2001')
select isdate('08-03-2002')
select isdate('08.03')
select isdate('08-03')
select isdate('2008/03/12')


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消