1.sysdate方法与trunc、to_char的应用
<1> sysdate是用于返回系统当前的时间: select sysdate from dual;
需要注意的是,当我们在查询语句中使用sysdate时,获取到的数据类型是日期类型,且sysdate返回的格式是yyyy/mm/dd hh24/mm/ss。
<2>当我们需要某些特定的时间时,需要使用to_char和trunc与sysdate搭配使用(共10种)
年:yyyy select to_char(sysdate,'yyyy') from dual;
月:mm select to_char(sysdate,'mm') from dual;
日:dd select to_char(sysdate,'dd') from dual;
时:hh 12时制 hh24 24小时制 select to_char(sysdate,'hh') from dual;
select to_char(sysdate,'hh24') from dual;
分:mi select to_char(sysdate,'mi') from dual;
秒:ss select to_char(sysdate,'ss') from dual;
ddd 年中的第几天
WW 年中的第几个星期
W 该月中第几个星期
D 周中的星期几
select to_char(sysdate,'ddd') from dual;
select to_char(sysdate,'WW') from dual;
select to_char(sysdate,'W') from dual;
select to_char(sysdate,'D') from dual;
上一篇文章说过to_char不会增加默认值
<3> trunc()函数
select trunc(sysdate,'格式') from dual;
可以通过截取相应格式的值来进行值的返回。
trunc会增加默认值 即若run select trunc(sysdate,'yyyy') from dual;
trunc会在截取后增加默认值yyyy/1/1
mm同理:yyyy/mm/1
即使用trunc可以返回当前yyyy年的第一天或者yyyy年mm月的第一天等
hh24/hh:mi:ss 默认的是00.、
trunc()还可以对number进行截取:trunc(number,'fmt')
主要用法是对number进行精度的控制 当fmt>0时,number将会保留fmt位小数
当fmt=0时,number将不保留小数部分
当fmt<0时,number将保留-fmt位整数(其余位数变0)
2.order by查询
使用如下语句进行查询:
<1> select cloumn_name1,cloumn_name2,cloumn_name3,..., from table_name order by number;
number数量<=cloumn_namex的值,即按照这一列参数进行排序
<2> 此外,null值在排序时自动被定义为极大值,默认将排在最后,可使用null first/last 进行调整.