a.substring
b.LEN
查找:串内搜索
charindex(char_expr,expression)
返回指定char_expr的开始位置,否则为0
select charindex('C',job_id) from job_z where job_id='1'
c.字符串连接:select substring('1',1,LEN('1')-2)||'--' from job_tmp
d.ltrim与ltrim
e.select case when job_id='8 '
then 'xx' else '--' end a
from job_tmp
f.if 1=2 select count(*) from job_t else select count(*) from job_tmp
g.-select convert (varchar,12)
--select convert (nvarchar,100)
--select convert (char,256)
--select convert (int,'12') 数字
select convert (float,'20')
--select convert(date ,'20120131') 日期
--select convert(datetime,'2012-1-31')
--select convert(numeric,2345.56666666678)
--select convert(float,1234567890123456789)
--select convert(numeric(30,2),'123456789.12') 小数
select convert(decimal(20,2),'12345.67') 小数
转换函数:
select convert(varchar,getdate(),112) 日期转换成字符串:20120301
select convert(date,'20120301',112) 字符转换成日期:
h.getdate() 系统时间
i.len()与datalength()的区别:datalength计算空格. char_length()也计算空格
j.加空格select space(5)||'-'
k.select replicate('abc',3) : 字符串abc重复3次
m.select stuff('abcdefghijkab',1,2,'AB') :把ab替换成AB
l.
select reverse('abcdefghijk') :反转字符串
n.转义字符:[]
o.数学函数:
select ceiling(1234.56) 数字四舍五入
select floor(1234.86) 取整数
select rand() 0-1之间的小数
select round(2.2345678,2) 2.2300000 四舍五入
p.select pi()
q.对空值的处理:select isnull(null,0)
r.
--select day(getdate())
--select month(getdate())
--select year(getdate())
--select datepart(yy,getdate())
--select convert(varchar(24),getdate(),112) 20120301
--select convert(varchar(24),getdate(),111) 2012/03/01
--select convert(varchar,getdate(),118)
--select datepart(mi,getdate()) 分钟
--select datepart(ms,getdate()) 毫秒
s.时间之间的换算:
select dateadd(dd,1,getdate()) 加一天
select datediff(dd,getdate(),dateadd(dd,1,getdate())) 两个日期比校,相差一天
datepart
日期部件 缩写 值范围
年 yy 1753-9999
季度 qq 1-4
月 mm 1-12
每年中的天 dy 1-366
天 dd 1-31
星期 wk 1-54
星期天 dw 1-7(1=sunday)
小时 hh 0-23
分钟 mi 0-59
秒 ss 0-59
毫秒 ms 0-999
t.替换: stuff(expr1,start,length,expr2)
用expr2代替epxr1中start起始长为length的字符串
select stuff(job_id,charindex('1',job_id),2,'38') from job_z where job_id='1'
u.字符串截取,日期截取: