SQL server中字符串函数

charindex()

查询某个字符,第一次出现的位置,注意:位置/索引 从 1 开始不存在返回0

select charindex('bc','abcdbc') 
select charindex('5','56489755')
select charindex('活',BookName) from BookInfos

patindex()

返回某个字符串第一次出现位置。注意:字符片段必须有 %string%

select patindex('%bc%','abcdbc')  -- 2
select patindex('%快乐%','什么是快乐星球')  --4
select patindex('%bc%','admin')   -- 0  不存在

ppe()

将小写字母边大写

select upper('abcDDD')

lower()

将大写字母变小写

select lower('abcDDD')

ltrim()

去除首位空格

select ltrim('       adm   in        ')
--结果:'adm   in        ' 

rtrim()

去除末尾空格

select ltrim('       adm   in        ')
--结果:'       adm   in' 

len()

查询字符串长度。

select len('abc')

reverse()

颠倒字符串

select reverse('abcdefg')

replace

替换字符

select replace('日本人,美国人,韩国人','人','狗')
select replace('2025/01/12','/','-')

Time相关的函数

--  getdate() 获取当前时间
select getdate();
-- 以下内容为日期的格式
select convert(varchar,getdate(),100)  --06 25 2025 11:40AM
select convert(varchar,getdate(),120)    -- 2025-06-25 11:40:24
select convert(varchar(20),getdate(),20)  -- 2025-06-25 11:40:56
select convert(varchar(20),getdate(),101)  -- 06/25/2025
select convert(varchar(20),getdate(),102)   -- 2025.06.25
select convert(varchar(20),getdate(),103)   -- 25/06/2025
select convert(varchar(20),getdate(),104)   -- 25.06.2025
select convert(varchar(20),getdate(),105)   -- 25-06-2025
select convert(varchar(20),getdate(),106)   -- 25 06 2025
select convert(varchar(20),getdate(),107)   -- 06 25, 2025
select convert(varchar(20),getdate(),108)   -- 11:41:51
select convert(varchar(20),getdate(),110)   -- 06-25-2025
select convert(varchar(20),getdate(),111)   -- 2025/06/25
-- 常用
select convert(varchar(20),getdate(),112)   -- 20250625
select convert(varchar(30),getdate(),121)   -- 2025-06-25 11:42:10.090

-- 分别获取,当前时间的 年,月,日,时
select datepart(day,getDate())
select datepart(month,getDate())
select datepart(year,getDate())
select datepart(Hour,getDate())
select datepart(month,'2021-07-20')

-- dataadd() 添加时间,
-- 参数: 1 mouth day year hour,2 添加数量,3 时间字符串。
select dateadd(month,2,getdate()) --2025-08-25 14:06:23.803
select dateadd(month,2,'2024-01-01')  -- 2024-03-01 00:00:00.000

-- datediff 求时间差
-- 求两个时间相差多少天
select datediff(day,'2024-02-01',getdate())
select datediff(month,'2024-02-01',getdate())
select datediff(year,'2024-02-01',getdate())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值