SQL语句大全(二)-- 字符串,数字,时间的操作

字符串操作 

/*插入字符串*/
insert into string_tbl(char_fld,vcharfld,text_fld)
values('This is char data','This is varchar data','this is text data');
/*更新字符串*/
update string_tbl
set vchar_fld=''
/*使用quote()函数为增加单引号和转义符*/
select quote(text_fid) from string_tbl;
/*删除表*/
delete from string_tbl;
/*返回字符串长度*/
select length(char_fid) char_length,
  length(varchar_fid) varchar_length,
  length(text_fid) text_length
from string_tbl;
/*查找子字符串的位置,使用position()函数*/
select position('character' in vchar_id)
from string_tbl;
/*查找子字符串位置时,自定义起始搜索位置使用
local()函数,比如查找第5个字符之后'is'出现的位置*/
select locale('is',vchar_fld,5)
from string_tbl;
/*使用like, regexp进行模式匹配,返回结果为1(true)或0(False)*/
select name,name like'%ns' ends_in_ns from department; --判断是否以ns结尾
select cust_id,cust_type_cd,fed_id,
  fed_id regexp '.{3}-.{2}-.{4}' is_ss_no_format
from customer;
/*用concat()函数向已存储的字符串后面附加额外的子符*/
update string_tbl
set text_fld=concat(text_fld,',but now it is longer');
select concat(fname,lname) from employee;
/*insert()函数用于替换字符串中的子字符串,四个参数分别表示
原始字符串,字符串操作的开始位置,需要替换的字符数以及替换字符串*/
select insert ('goodbye world', 9,0,'cruel') string;
/*substring()函数从指定的位置开始提取指定数目的字符,比如从
第9个位置提取5个字符*/
select substring('goodbye cruel world',9,5);

数值操作

/*执行数值计算*/
select (37*59)/(78-(8*6));
/*执行算术函数
三角函数:Sin(x),Cos(x),Tan(x),Cot(x)
反三角函数:Acos(x),Asin(x),Atan(x)
指数函数:Exp(x)
对数函数:Ln(x)
平方根:Sqrt(x)*/
/*利用mod()函数求模,即计算两数相除所得余数*/
select mod(10,4); --返回2
/*利用pow()函数返回两个参数的幂计算*/
select pow(2,8); --返回256
/*ceil()向上取整,floor()向下取整,round()以四舍五入的方式取整*/
select ceil(72.445),floor(72.445),round(72.445);
/*round()还可以指定在小数点右侧保留多少位(四舍五入)*/
select round(72.0909,1),round(72.0909,2),round(72.0909,3);
/*truncate()函数也允许接受第二个可选参数,以指定小数点右侧保留的位数
但truncate()只是简单的去掉不需要的小数位,而不进行四舍五入*/
select truncate(72.0909,1),truncate(72.0909,2),truncate(72.0909,3);
-- 72.0,72.09,72.090
/*truncate()和round()函数都可以为第二个参数指定一个负数,表示小数点左侧
需要被截取或取整多少位*/
select round(17,-1),truncate(17,-1);
-- 20,10
/*sign()函数返回符号,正数是1,负数是-1,0则返回0,asb()返回绝对值*/
select account_id,sign(avail_balance),abs(avail_balance) from account;

时间操作

/*可使用cast()将字符串转化为datetime*/
select cast('2018-09-07 15:30:00' as datetime);
select cast('2018-09-07' as date) date_field,
  cast('108:17:57' as time) time_field;
/*如果字符串并非需要的YYYY-MM-DD格式,可以使用str_to_date()格式化
再使用cast()函数*/
update individual
set birth_date=str_to_date('September 17,2008','%M %d,%Y')
where cust_id=9999;
--str_to_date()函数根据格式字符串的内容自动返回datetime,date和time类型值。
/*date_add用于计算增加天数后的日期,比如为当前日期增加5天*/
select date_add(current_date(),interval 5 day); --注意使用interval关键字
/*常用的时间间隔类型
second 秒数;
minute 分钟数;
hour 小时数;
day 天数;
month 月份;
year 年份;
minute_second 分钟数:秒数
hour_second 小时数:分钟数:秒数
year_month 年份-月份*/
update transaction
set txn_date=date_add(txn_date,interval'3:27:11'hour_second)
where emp_id=4789;
/*dayname()函数可以确定某一日期是星期几*/
select dayname('2008-09-18');
/*使用extract()函数提取日期值中得信息,它使用与date_add()函数相同
的时间间隔类型*/
select extract(year from '2008-09-18 22:19:05');
/*使用datediff()计算两个日期之间的间隔天数*/
select datediff('2009-09-03','2009-06-24'); --71
/*转化函数cast()可以在任意类型间相互转换,不仅仅是时间*/
select cast('1456328' as signed integer); --1456328

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值