SQL Server学习(三)

185 阅读1分钟

数据库函数学习

聚合函数

avg()
sum()
max()
min()

selcet avg(score) from student

count() --2^23-1
count_big()
len() --返回指定字符串表达式的字符数;不包含尾随空格
datalength() --返回用于表示表达式的字节数

随机数

rand() select floor(rand() * N)
select ceiling(rand() * N)

获取时间

Getdate()
getutcdate()

时间转换函数

convert() --把日期转换为新数据类型的通用函数;可以用不同的格式显示日期/时间数据
convert(data_type(length),data_to_converted,style)

Style IDStyle格式
100或者0mon dd yyyy hh:mmAM(或者PM)

datediff()
dateadd() datepart() datename()

字符串

charindex() partindex()

--stuff(列名,开始位置,长度,替代字符串) --substring()截取指定长度的字符串

--substring(expression,start,length)

--select substring('abcdefg',3,5)

--select left('abcdefg',4) --select right('abcdefg',3)

ltrim()去左边空格 rtrim()

select LTRIM(' abcd ') select RTRIM(' abcd ') select LTRIM(RTRIM(' abcde '))

upper()

select upper('abcdefg')

select lower('ABCdEf')

select replace('abcabcabc','b','have')

replicate()

space()

select replicate('abc',4)

select '1'+SPACE(5)+'h'

select upper(reverse('abcdefg'))

select cast(123 as varchar(5))

decimal()

case()