SQL 常用函数
- CONCAT 连接函数
- 分组排序函数
- LEN
- RIGHT
- LEFT
- SUBSTRING
- ISNULL
- COALESCE
- GETDATE
- YEAR
- MONTH
- DAY
- ISDATE
- DATENAME
- DATEPART
- PARSENAME
- QUOTENAME
- REPLICATE
- REVERSE
- SPACE
- ASCII
- UNICODE
- CHAR
- NCHAR
- REPLACE
- RTRIM
- LTRIM
- UPPER
- LOWER
- STUFF
- CHARINDEX
- SOUNDEX
- DIFFERENCE
- STR
- PATINDEX
CONCAT 连接函数
SELECT CONCAT(N'A', N'B')
SELECT CONCAT_WS(N'_',N'A', N'B')
分组排序函数
- ROW_NUMBER() OVER (PARTITION BY 分组字段 ORDER BY 排序字段 DESC)
SELECT *, ROW_NUMBER() OVER (PARTITION BY [性别] ORDER BY [入职时间] DESC) [Rank] FROM [Test].[dbo].[Test.Source]
- 分组排序前:
- 分组排序后:
LEN
SELECT LEN(N'ABC')
SELECT LEN(N' ABC')
SELECT LEN(N'ABC ')
RIGHT
SELECT RIGHT(N'TEST', 2)
LEFT
SELECT LEFT(N'TEST', 2)
SUBSTRING
SELECT SUBSTRING(N'TEST', 2, 2)
ISNULL
SELECT ISNULL(N'', 'A')
SELECT ISNULL(NULL, 'A')
SELECT ISNULL(N'A', 'B')
SELECT ISNULL(NULL, NULL)
COALESCE
SELECT c(N'A', N'')
SELECT COALESCE(NULL, N'')
SELECT COALESCE(NULL, NULL, N'A', N'B')
GETDATE
SELECT GETDATE()
YEAR
SELECT YEAR(N'2022-03-16')
SELECT YEAR(N'2022-03-16 11:00:05.800')
SELECT YEAR(GETDATE())
MONTH
SELECT MONTH(N'2022-03-16')
SELECT MONTH(N'2022-03-16 11:00:05.800')
SELECT MONTH(GETDATE())
DAY
SELECT DAY(N'2022-03-16')
SELECT DAY(N'2022-03-16 11:00:05.800')
SELECT DAY(GETDATE())
ISDATE
SELECT ISDATE(N'2022-03-16')
SELECT ISDATE(N'03-16-2022')
SELECT ISDATE(N'16-2022-03')
SELECT ISDATE(N'16-03-2022')
SELECT ISDATE(N'2022-03-16 11:00:05.800')
SELECT ISDATE(N'2022-03-16 11:00:05.800')
SELECT ISDATE(N'2022-02-29')
DATENAME
SELECT DATENAME(YEAR, N'2022-03-16 11:00:05.800')
SELECT DATENAME(YY, N'2022-03-16 11:00:05.800')
- 缩写对应表格
DATEPART
SELECT DATEPART(HH, N'2022-03-16 11:00:05.800')
PARSENAME
SELECT PARSENAME(N'192.168.0.1', 1) COLUMN4
,PARSENAME(N'192.168.0.1', 2) COLUMN3
,PARSENAME(N'192.168.0.1', 3) COLUMN2
,PARSENAME(N'192.168.0.1', 4) COLUMN1
QUOTENAME
SELECT QUOTENAME(N'A', N'')
SELECT QUOTENAME(N'A', N'[]')
SELECT QUOTENAME(N'A', N'''') --返回'A'
SELECT QUOTENAME(N'A', N'""') --返回"A"
SELECT QUOTENAME(N'A[]B', N'[]')
REPLICATE
SELECT REPLICATE(N'A', 3)
SELECT REPLICATE(N'AB', 3)
REVERSE
SELECT REVERSE(N'123')
SELECT REVERSE(N'ABC')
SPACE
SELECT N'1'+SPACE(5)+N'2'
SELECT LEN(N'A'+SPACE(5)+N'B')
ASCII
SELECT ASCII(N'A')
SELECT ASCII(N'AB')
UNICODE
SELECT UNICODE(N'A')
SELECT UNICODE(N'ABC')
CHAR
SELECT CHAR(65)
SELECT CHAR(256)
NCHAR
SELECT NCHAR(N'65')
REPLACE
SELECT REPLACE(N'ABCB', N'B', N'D')
SELECT REPLACE(N'ABBC', N'B', N'')
RTRIM
SELECT RTRIM(N'ABC ')
SELECT LEN(RTRIM(N'ABC '))
LTRIM
SELECT LTRIM(N' ABC')
SELECT LEN(N' ABC')
SELECT LEN(LTRIM(N' ABC'))
UPPER
SELECT UPPER(N'test')
SELECT UPPER(N'1024')
LOWER
SELECT LOWER(N'TEST')
SELECT LOWER(N'1024')
STUFF
SELECT STUFF(N'ABCD', 2, 2, N'EE')
SELECT STUFF(N'ABCD', 2, 2, N'')
CHARINDEX
SELECT CHARINDEX(N'0', N'1024')
SELECT CHARINDEX(N'5', N'1024')
SELECT CHARINDEX(N'ES', N'TEST')
SOUNDEX
SELECT SOUNDEX(N'TEST')
DIFFERENCE
SELECT DIFFERENCE(N'TEST', N'1024')
SELECT DIFFERENCE(N'TEST', N'TEST')
SELECT DIFFERENCE(N'SQL', N'TEST')
STR
SELECT STR(1024)
SELECT LEN(STR(1024))
SELECT STR(1024, 15)
SELECT LEN(STR(1024, 15))
SELECT STR(1024, 20, 2)
SELECT LEN(STR(1024, 20, 2))
PATINDEX
SELECT PATINDEX(N'TEST', N'TEST1024TEST')
SELECT PATINDEX(N'TEST', N'TEST')
SELECT PATINDEX(N'TEST%', N'1024TEST1024')
SELECT PATINDEX(N'%TEST%', N'1024TEST1024TEST1024')
SELECT PATINDEX(N'%[TEST]%', N'1024TEST1024TEST1024')
SELECT PATINDEX(N'%[^TEST]%', N'1024TEST1024TEST1024')
SELECT PATINDEX(N'%[^TEST]%', N'T1024TEST1024TEST1024')