sql 截取日期
时间:2021-07-01 10:21:17
帮助过:10人阅读
select to_char( NEW_TIME( sysdate, ‘GMT‘,‘EST‘), ‘yyyy-mm‘)from dual;
或得年或月或日 Year/ month/Day
select to_char(to_date(‘2016-01-01‘,‘yyyy-mm-dd‘),‘month‘) from dual;
-------------------------------------------华丽的分割符----------------------------------------------------------
Year:
yy two digits 两位年 显示值:07
yyy three digits 三位年 显示值:007
yyyy four digits 四位年 显示值:2007
Month:
mm number 两位月 显示值:11
mon abbreviated 字符集表示 显示值:11月,若是英文版,显示nov
month spelled out 字符集表示 显示值:11月,若是英文版,显示november
Day:
dd number 当月第几天 显示值:02
ddd number 当年第几天 显示值:02
dy abbreviated 当周第几天简写 显示值:星期五,若是英文版,显示fri
day spelled out 当周第几天全写 显示值:星期五,若是英文版,显示friday
Hour:
hh two digits 12小时进制 显示值:01
hh24 two digits 24小时进制 显示值:13
Minute:
mi two digits 60进制 显示值:45
Second:
ss two digits 60进制 显示值:25
其它
Q digit 季度 显示值:4
WW digit 当年第几周 显示值:44
W digit 当月第几周 显示值:1
24小时格式下时间范围为: 0:00:00 - 23:59:59....
12小时格式下时间范围为: 1:00:00 - 12:59:59....
1. 日期和字符转换函数用法(to_date,to_char)
1
2
3
4
5
6
7
|
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ) as nowTime from dual;
select to_char(sysdate, ‘yyyy‘ ) as nowYear from dual;
select to_char(sysdate, ‘mm‘ ) as nowMonth from dual;
select to_char(sysdate, ‘dd‘ ) as nowDay from dual;
select to_char(sysdate, ‘hh24‘ ) as nowHour from dual;
select to_char(sysdate, ‘mi‘ ) as nowMinute from dual;
select to_char(sysdate, ‘ss‘ ) as nowSecond from dual;
|
2. 字符串和时间互转
1
2
|
select to_date( ‘2004-05-07 13:23:44‘ , ‘yyyy-mm-dd hh24:mi:ss‘ ) from dual
select to_char( to_date( 222 , ‘J‘ ), ‘Jsp‘ ) from dual
|
3.求某天是星期几
1
2
3
4
5
6
7
|
select to_char(to_date( ‘2002-08-26‘ , ‘yyyy-mm-dd‘ ), ‘day‘ ) from dual;
select to_char(to_date( ‘2002-08-26‘ , ‘yyyy-mm-dd‘ ), ‘day‘ ,
‘NLS_DATE_LANGUAGE = American‘ ) from dual;
ALTER SESSION SET NLS_DATE_LANGUAGE= ‘AMERICAN‘ ;
TO_DATE ( ‘2002-08-26‘ , ‘YYYY-mm-dd‘ , ‘NLS_DATE_LANGUAGE = American‘ )
|
4. 两个日期间的天数
1
|
select floor(sysdate - to_date( ‘20020405‘ , ‘yyyymmdd‘ )) from dual;
|
5. 时间为null的用法
1
2
3
|
select id, active_date from table1
UNION
select 1 , TO_DATE( null ) from dual;
|
6.月份差
1
2
3
|
a_date between to_date( ‘20011201‘ , ‘yyyymmdd‘ ) and to_date( ‘20011231‘ , ‘yyyymmdd‘ )
|
7. 日期格式冲突问题
输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: ‘01-Jan-01‘
1
2
3
4
5
6
7
8
|
alter system set NLS_DATE_LANGUAGE = American
alter session set NLS_DATE_LANGUAGE = American
select to_char(to_date( ‘2002-08-26‘ , ‘yyyy-mm-dd‘ ),
‘day‘ , ‘NLS_DATE_LANGUAGE = American‘ ) from dual;
select * from nls_session_parameters
select * from V$NLS_PARAMETERS
|
8.查询特殊条件天数
1
2
3
4
5
6
7
8
9
10
11
|
select count(*)
from ( select rownum- 1 rnum
from all_objects
where rownum <= to_date( ‘2002-02-28‘ , ‘yyyy-mm-dd‘ ) - to_date(‘ 2002 -
02 - 01 ‘,‘ yyyy-mm-dd‘)+ 1
)
where to_char( to_date( ‘2002-02-01‘ , ‘yyyy-mm-dd‘ )+rnum- 1 , ‘D‘ )
not in ( ‘1‘ , ‘7‘ )
|
9. 查找月份
1
2
3
4
5
6
|
select months_between(to_date( ‘01-31-1999‘ , ‘MM-DD-YYYY‘ ),
to_date( ‘12-31-1998‘ , ‘MM-DD-YYYY‘ )) "MONTHS" FROM DUAL;
select months_between(to_date( ‘02-01-1999‘ , ‘MM-DD-YYYY‘ ),
to_date( ‘12-31-1998‘ , ‘MM-DD-YYYY‘ )) "MONTHS" FROM DUAL;
|
10. Next_day的用法
1
2
3
4
|
Next_day(date, day)
Monday-Sunday, for format code DAY
Mon-Sun, for format code DY
1 - 7 , for format code D
|
11.获得小时数
1
2
3
4
5
6
7
8
9
10
11
12
13
|
SELECT EXTRACT(HOUR FROM TIMESTAMP ‘2001-02-16 2:38:40‘ ) from offer
select sysdate ,to_char(sysdate, ‘hh‘ ) from dual;
SYSDATE TO_CHAR(SYSDATE, ‘HH‘ )
-------------------- ---------------------
2003 - 10 - 13 19 : 35 : 21 07
select sysdate ,to_char(sysdate, ‘hh24‘ ) from dual;
SYSDATE TO_CHAR(SYSDATE, ‘HH24‘ )
-------------------- -----------------------
2003 - 10 - 13 19 : 35 : 21 19
|
12.年月日的处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
SELECT
older_date,
newer_date,
years,
months,
ABS (
TRUNC (
newer_date - ADD_MONTHS (older_date, years * 12 + months)
)
) days
FROM
(
SELECT
TRUNC (
MONTHS_BETWEEN (newer_date, older_date) / 12
) YEARS,
MOD (
TRUNC (
MONTHS_BETWEEN (newer_date, older_date)
),
12
) MONTHS,
newer_date,
older_date
FROM
(
SELECT
hiredate older_date,
ADD_MONTHS (hiredate, ROWNUM) + ROWNUM newer_date
FROM
emp
)
)
|
13.处理月份天数不定的办法
1
|
select to_char(add_months(last_day(sysdate) + 1 , - 2 ), ‘yyyymmdd‘ ),last_day(sysdate) from dual
|
14.找出今年的天数
1
2
3
4
|
select add_months(trunc(sysdate, ‘year‘ ), 12 ) - trunc(sysdate, ‘year‘ ) from dual
to_char( last_day( to_date( ‘02‘ | | :year, ‘mmyyyy‘ ) ), ‘dd‘ )
|
15.yyyy与rrrr的区别
1
2
3
4
5
6
|
YYYY99 TO_C
------- ----
yyyy 99 0099
rrrr 99 1999
yyyy 01 0001
rrrr 01 2001
|
16.不同时区的处理
1
2
|
select to_char( NEW_TIME( sysdate, ‘GMT‘ , ‘EST‘ ), ‘dd/mm/yyyy hh:mi:ss‘ ) ,
sysdate from dual;
|
17. 5秒钟一个间隔
1
2
3
4
|
Select TO_DATE(FLOOR(TO_CHAR(sysdate, ‘SSSSS‘ )/ 300 ) * 300 , ‘SSSSS‘ ) ,
TO_CHAR(sysdate, ‘SSSSS‘ ) from dual
|
18.一年的第几天
1
2
|
select TO_CHAR(SYSDATE, ‘DDD‘ ),sysdate from dual
|
19.计算小时,分,秒,毫秒
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
SELECT
Days,
A,
TRUNC (A * 24 ) Hours,
TRUNC (A * 24 * 60 - 60 * TRUNC(A * 24 )) Minutes,
TRUNC (
A * 24 * 60 * 60 - 60 * TRUNC (A * 24 * 60 )
) Seconds,
TRUNC (
A * 24 * 60 * 60 * 100 - 100 * TRUNC (A * 24 * 60 * 60 )
) mSeconds
FROM
(
SELECT
TRUNC (SYSDATE) Days,
SYSDATE - TRUNC (SYSDATE) A
FROM
dual
) SELECT
*
FROM
tabname
ORDER BY
DECODE (MODE, ‘FIFO‘ , 1 ,- 1 ) * TO_CHAR (rq, ‘yyyymmddhh24miss‘ )
|
20.next_day函数
1
2
3
4
5
6
|
next_day(sysdate, 6 )是从当前开始下一个星期五。后面的数字是从星期日开始算起。
select (sysdate-to_date( ‘2003-12-03 12:55:45‘ , ‘yyyy-mm-dd hh24:mi:ss‘ ))* 24 * 60 * 60 from dual
|
21,round[舍入到最接近的日期](day:舍入到最接近的星期日)
1
2
3
4
5
|
select sysdate S1,
round(sysdate) S2 ,
round(sysdate, ‘year‘ ) YEAR,
round(sysdate, ‘month‘ ) MONTH ,
round(sysdate, ‘day‘ ) DAY from dual
|
22,trunc[截断到最接近的日期,单位为天] ,返回的是日期类型
1
2
3
4
5
6
|
select sysdate S1,
trunc(sysdate) S2,
trunc(sysdate, ‘year‘ ) YEAR,
trunc(sysdate, ‘month‘ ) MONTH ,
trunc(sysdate, ‘day‘ ) DAY
from dual
|
23,返回日期列表中最晚日期
1
|
select greatest( ‘01-1月-04‘ , ‘04-1月-04‘ , ‘10-2月-04‘ ) from dual
|
24.计算时间差
1
2
3
4
5
6
7
8
9
10
11
12
13
|
注:oracle时间差是以天数为单位,所以换算成年月,日
select floor(to_number(sysdate-to_date( ‘2007-11-02 15:55:03‘ ,
‘yyyy-mm-dd hh24:mi:ss‘ ))/ 365 ) as spanYears from dual
select ceil(moths_between(sysdate-to_date( ‘2007-11-02 15:55:03‘ ,
‘yyyy-mm-dd hh24:mi:ss‘ ))) as spanMonths from dual
select floor(to_number(sysdate-to_date( ‘2007-11-02 15:55:03‘ ,
‘yyyy-mm-dd hh24:mi:ss‘ ))) as spanDays from dual
select floor(to_number(sysdate-to_date( ‘2007-11-02 15:55:03‘ ,
‘yyyy-mm-dd hh24:mi:ss‘ ))* 24 ) as spanHours from dual
select floor(to_number(sysdate-to_date( ‘2007-11-02 15:55:03‘ ,
‘yyyy-mm-dd hh24:mi:ss‘ ))* 24 * 60 ) as spanMinutes from dual
select floor(to_number(sysdate-to_date( ‘2007-11-02 15:55:03‘ ,
‘yyyy-mm-dd hh24:mi:ss‘ ))* 24 * 60 * 60 ) as spanSeconds from dual
|
25.更新时间
1
2
3
4
5
6
7
8
9
10
11
12
13
|
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ),
to_char(sysdate+n* 365 , ‘yyyy-mm-dd hh24:mi:ss‘ ) as newTime from dual
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ),
add_months(sysdate,n) as newTime from dual
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ),
to_char(sysdate+n, ‘yyyy-mm-dd hh24:mi:ss‘ ) as newTime from dual
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ),
to_char(sysdate+n/ 24 , ‘yyyy-mm-dd hh24:mi:ss‘ ) as newTime from dual
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ),
to_char(sysdate+n/ 24 / 60 , ‘yyyy-mm-dd hh24:mi:ss‘ ) as newTime from dual
select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘ ),
to_char(sysdate+n/ 24 / 60 / 60 , ‘yyyy-mm-dd hh24:mi:ss‘ ) as newTime from dual
|
26.查找月的第一天,最后一天
1
2
3
4
5
|
SELECT Trunc(Trunc(SYSDATE, ‘MONTH‘ ) - 1 , ‘MONTH‘ ) First_Day_Last_Month,
Trunc(SYSDATE, ‘MONTH‘ ) - 1 / 86400 Last_Day_Last_Month,
Trunc(SYSDATE, ‘MONTH‘ ) First_Day_Cur_Month,
LAST_DAY(Trunc(SYSDATE, ‘MONTH‘ )) + 1 - 1 / 86400 Last_Day_Cur_Month
FROM dual;
|
sql 截取日期
标签: