现在的位置: 首页 > 综合 > 正文

sql语句1

2012年02月17日 ⁄ 综合 ⁄ 共 4016字 ⁄ 字号 评论关闭

//distinct 消去重复记录
select distinct class_id from NEWS;

//count() 统计记录数
select count(distinct class_id) from NEWS;

//concat('','') 或 || 连接字符串
select '新闻标题:' || title 新闻标题 , concat( '新闻内容:' , content) 新闻内容 from News;

//between...and...
select * from NEWS where to_char(PUBDATE,'yyyy-mm-dd') between '2012-11-10' and '2012-12-31';

//is not null 不为空 记录
select * from NEWS where CONTENT IS NOT NULL ;

//is null 为空 记录
select * from NEWS where CONTENT IS NULL ;

//and 语句
select * from NEWS where content IS NULL and class_id='3';

//or 语句
select * from NEWS where content is null or class_id='3';

//not 取反语句
select * from NEWS where not( content IS NULL and class_id='3');

//in 语句
select * from NEWS where class_id in ('3','4','5');

//not in 语句
select * from NEWS where class_id not in ('3','4','5');

//link 语句 “%”可以匹配任意长度的内容,“_”可以匹配一个长度的内容
select * from NEWS where title like'%光%';

//<> 和 != 不等号
select count(*) from NEWS where class_id != '3';
select count(*) from NEWS where class_id <> '3';

//order by ASC升序 DESC降序
select * from NEWS order by pubdate desc
select * from NEWS order by pubdate desc, class_id asc

//字符函数--------------------------------------------------------------------------------------------

//upper() 将字符串变为大写字母
select upper('ZhangZe') from dual; 结果:ZHANGZE

//lower() 将字符串变为小写字母 结果:zhangze
select lower('ZHANGZE') from dual;

//initcap() 将单词的首字母大写
select initcap('zhang ze') from dual; 结果:Zhang Ze

//substr() 字符串截取
select substr('hello',1,3) from dual; 结果:hel
select substr('hello',0,3) from dual; 结果:hel
//采用倒着截取的方式,只要输入的位置是负数就表示倒着进行
select substr('hello',-3,3) from dual; 结果:llo

//length() 字符串长度
select length('hello') from dual; 结果:5

//replace() 字符串替换
select replace('hello','l','x') from dual; 结果:hexxo

//数值函数-----------------------------------------------------------------------------------

//round() 四舍五入
select round(120.56) from dual; 结果:121
select round(120.16) from dual; 结果:120
//保留两位小数点
select round(120.567,2) from dual; 结果:120.57
//对整数进行四舍五入的进位
select round(120.567,-2) from dual; 结果:100
select round(156.567,-2) from dual; 结果:200
select round(156.567,-3) from dual; 结果:0
select round(556.567,-3) from dual; 结果:1000

//trunc() 截取小数位
select trunc(120.56) from dual; 结果:120
//指定小数点的保留位数
select trunc(120.56 ,1) from dual; 结果:120.5
//使用负数表示位数
select trunc(129.56,-2) from dual; 结果:100

//mod() 取余
select mod(10,3) from dual; 结果:1
select mod(10,2) from dual; 结果:0

//日期函数--------------------------------------------------------------------------------------------
日期进行加或减的规律
日期 - 数字 = 日期
日期 + 数字 = 日期
日期 - 日期 = 数字
//sysdate 当前日期
select sysdate from dual;
select round((sysdate - pubdate)/7) from NEWS;

//months_between() 求出给定日期范围的月数
select months_between(sysdate,pubdate) from NEWS;

//add_months() 在指定日期上加上指定的月数,求出之后的日期
select add_months(sysdate,4) from dual;

//next_day() 求出给定的周在下一次出现的日期
select next_day(sysdate,'星期四') from dual;

//last_day() 求出给定日期的最后一天日期
select last_day(sysdate) from dual;

//转换函数---------------------------------------------------------------------------------------------------

//to_char() 转换成字符串
//用于日期
select to_char(sysdate,'yyyy') from dual; 结果:2012
select to_char(sysdate,'mm') from dual; 结果:11
select to_char(sysdate,'dd') from dual; 结果:22
select to_char(sysdate,'yyyy-mm-dd') from dual ; 结果: 2012-11-22
select to_char(sysdate,'yyyymmdd') from dual ; 结果: 20121122
select to_char(sysdate,'yyyy/mm/dd') from dual ; 结果: 2012/11/22
select to_char(sysdate,'yyyy*mm*dd') from dual ; 结果: 2012*11*22
//用于数字,$表示美元,L表示Local的缩写,以本地的语言进行金额的显示
select to_char('20202020','99,999,999') from dual; 结果: 20,202,020
select to_char('20202020','$99,999,999') from dual; 结果: $20,202,020
select to_char('20202020','L99,999,999') from dual; 结果: ¥20,202,020

//to_number() 转换成数字
select to_number('123')+to_number('321') from dual; 结果:444
select '123'+'321' from dual; 结果:444

//to_date() 转换成日期
select to_date('2012-11-22','yyyy-mm-dd') from dual; 结果:2012/11/22

//通用函数---------------------------------------------------------------------------------------------------
//null 在参加计算之后的结果还是 null 为了避免计算中出现此情况(如:5+null=null)则使用 nvl()
//nvl() 可以将null 值变为指定的内容
select (sal+nvl(comm,0))*12 income from emp;

//decode() 函数类似于if...elseif...else语句。
//语法 decode(col/expression,search1,result1[,search2,result2,...][,default])
//说明 col/expression:为列明或表达式
search1、search2、...、searchN:为比较的条件
result1、result2、...、resultN:为返回值
如果col/expression 和 searchN相比较,结果相同的话,则返回resultN,如果没有与col/expression相匹配的条件,则返回默认值default。
select decode(2,1,'内容是1',2,'内容是2',3,'内容是3','内容是null') from dual; 结果:内容是2

抱歉!评论已关闭.