(日期和时间戳添加年月周天小时)和(日期和时间戳值转换为 TO_CHAR 函数指定的格式)

加一小时

select to_char(sysdate,'MM/DD/YYYY HH:MI:SS'),

       to_char(sysdate + 1/24,'MM/DD/YYYY HH:MI:SS'),

       systimestamp,

       systimestamp + numtodsinterval(1,'hour')

from dual

少一块,重新生成一个吧

加一天 

select sysdate + 1 as add_day,

       systimestamp  +  numtodsinterval(1, 'day')

from dual

 

加一周

select sysdate + 7 as add_day,

       systimestamp  +  numtodsinterval(7, 'day')

from dual

加一月

select add_months(sysdate, 1) add_month,

       cast(add_months(systimestamp,1) as timestamp with time zone)

from dual

加一年

select add_months(sysdate, 12) add_month,

       cast(add_months(systimestamp,12) as timestamp with time zone)

from dual

使用 TO_CHAR 格式化日期和数字

iw 格式表示一年中的第几周,iyyy 格式表示基于 ISO 标准的 4 位年份。

WITH dates AS ( 

  SELECT date'2015-01-01' d FROM dual union 

  SELECT date'2015-01-10' d FROM dual union 

  SELECT date'2015-02-01' d FROM dual

SELECT d "Original Date",

       to_char(d, 'dd-mm-yyyy') "Day-Month-Year", 

       to_char(d, 'hh24:mi') "Time in 24-hr format", 

       to_char(d, 'iw-iyyy') "ISO Year and Week of Year"

FROM dates

此语句将日期和时间戳值转换为 TO_CHAR 函数中指定的格式。

WITH dates AS ( 

  SELECT date'2015-01-01' d FROM dual union 

  SELECT date'2015-01-10' d FROM dual union 

  SELECT date'2015-02-01' d FROM dual union

  SELECT timestamp'2015-03-03 23:44:32' d FROM dual union 

  SELECT timestamp'2015-04-11 12:34:56' d FROM dual  

SELECT d "Original Date",

       to_char(d, 'dd-mm-yyyy') "Day-Month-Year", 

       to_char(d, 'hh24:mi') "Time in 24-hr format", 

       to_char(d, 'iw-iyyy') "ISO Year and Week of Year",

       to_char(d, 'Month') "Month Name",

       to_char(d, 'Year') "Year" 

FROM dates

 

此语句从输入日期时间表达式中提取 EXTRACT 函数中指定的日期时间字段

WITH dates AS (  

  SELECT date'2015-01-01' d FROM dual union  

  SELECT date'2015-01-10' d FROM dual union  

  SELECT date'2015-02-01' d FROM dual union  

  SELECT timestamp'2015-03-03 23:44:32' d FROM dual union  

  SELECT timestamp'2015-04-11 12:34:56' d FROM dual   

)  

SELECT extract(minute from d) minutes, 

       extract(hour from d) hours, 

       extract(day from d) days, 

       extract(month from d) months, 

       extract(year from d) years 

FROM dates

 

WITH nums AS ( 

  SELECT 10 n FROM dual union 

  SELECT 9.99 n FROM dual union 

  SELECT 1000000 n FROM dual  --one million 

SELECT n "Input Number N", 

       to_char(n), 

       to_char(n, '9,999,999.99') "Number with Commas", 

       to_char(n, '0,000,000.000') "Zero-padded Number", 

       to_char(n, '9.9EEEE') "Scientific Notation" 

FROM nums

'X' 格式返回指定位数的十六进制值。如果指定的数字不是整数,则将其四舍五入到最接近的整数

WITH nums AS ( 

  SELECT 10 n FROM dual union 

  SELECT 9.99 n FROM dual union 

  SELECT .99 n FROM dual union 

  SELECT 1000000 n FROM dual  --one million 

SELECT n "号码", 

       to_char(n), 

       to_char(n, '9,999,999.99') "带逗号的数字",

       to_char(n, '0,000,000.000') "补零数", 

       to_char(n, '9.9EEEE') "科学计数法", 

       to_char(n, '$9,999,990.00') 货币, 

       to_char(n, 'X') "十六进制值"

FROM nums

WITH nums AS ( 

  SELECT 10 n FROM dual union 

  SELECT 9.99 n FROM dual union 

  SELECT .99 n FROM dual union 

  SELECT 1000000 n FROM dual  --one million 

SELECT n "Input Number N", 

       to_char(n), 

       to_char(n, '9,999,999.99') "Number with Commas", 

       to_char(n, '0,000,000.000') "Zero_padded Number", 

       to_char(n, '9.9EEEE') "Scientific Notation", 

       to_char(n, '$9,999,990.00') Monetary, 

       to_char(n, 'XXXXXX') "Hexadecimal Value" 

FROM nums

猜你喜欢

转载自blog.csdn.net/weixin_39568073/article/details/121265371