广告
返回顶部
首页 > 资讯 > 数据库 >DATETIME FORMATE(时间格式)
  • 330
分享到

DATETIME FORMATE(时间格式)

2024-04-02 19:04:59 330人浏览 泡泡鱼
摘要

来源于oracle官方文档,为了工作中方便使用,这里截取下来供参考:Http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004

来源于oracle官方文档,为了工作中方便使用,这里截取下来供参考:

Http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004.htm#i34924

Table 3-15 Datetime FORMat Elements

ElementTO_* datetime functions?Description
-
/
,
.
;
:
"text"

Yes

Punctuation and quoted text is reproduced in the result.

AD
A.D.

Yes

AD indicator with or without periods.

AM
A.M.

Yes

Meridian indicator with or without periods.

BC
B.C.

Yes

BC indicator with or without periods.

CC
SCC

Century.

  • If the last 2 digits of a 4-digit year are between 01 and 99 (inclusive), then the century is one greater than the first 2 digits of that year.

  • If the last 2 digits of a 4-digit year are 00, then the century is the same as the first 2 digits of that year.

For example, 2002 returns 21; 2000 returns 20.

D

Yes

Day of week (1-7). This element depends on the NLS territory of the session.

DAY

Yes

Name of day.

DD

Yes

Day of month (1-31).

DDD

Yes

Day of year (1-366).

DL

Yes

Returns a value in the long date format, which is an extension of the Oracle Database DATE format, determined by the current value of the NLS_DATE_FORMAT parameter. Makes the appearance of the date components (day name, month number, and so forth) depend on the NLS_TERRITORY and NLS_LANGUAGE parameters. For example, in the AMERICAN_AMERICA locale, this is equivalent to specifying the format 'fmDay, Month dd, yyyy'. In the GERMAN_GERMANY locale, it is equivalent to specifying the format 'fmDay, dd. Month yyyy'.

Restriction: You can specify this format only with the TS element, separated by white space.

DS

Yes

Returns a value in the short date format. Makes the appearance of the date components (day name, month number, and so forth) depend on the NLS_TERRITORY and NLS_LANGUAGE parameters. For example, in the AMERICAN_AMERICA locale, this is equivalent to specifying the format 'MM/DD/RRRR'. In the ENGLISH_UNITED_KINGDOM locale, it is equivalent to specifying the format 'DD/MM/RRRR'.

Restriction: You can specify this format only with the TS element, separated by white space.

DY

Yes

Abbreviated name of day.

E

Yes

Abbreviated era name (Japanese Imperial, ROC Official, and Thai Buddha calendars).

EE

Yes

Full era name (Japanese Imperial, ROC Official, and Thai Buddha calendars).

FF [1..9]

Yes

Fractional seconds; no radix character is printed. Use the X format element to add the radix character. Use the numbers 1 to 9 after FF to specify the number of digits in the fractional second portion of the datetime value returned. If you do not specify a digit, then Oracle Database uses the precision specified for the datetime data type or the data type's default precision. Valid in timestamp and interval formats, but not in DATE formats.

Examples: 'HH:MI:SS.FF'

SELECT TO_CHAR(SYSTIMESTAMP, 'SS.FF3') from DUAL;

FM

Yes

Returns a value with no leading or trailing blanks.

See Also: Additional discussion on this format model modifier in the Oracle Database SQL Language Reference

FX

Yes

Requires exact matching between the character data and the format model.

See Also: Additional discussion on this format model modifier in the Oracle Database SQL Language Reference

HH
HH12

Yes

Hour of day (1-12).

HH24

Yes

Hour of day (0-23).

IW

Week of year (1-52 or 1-53) based on the ISO standard.

IYY
IY
I

Last 3, 2, or 1 digit(s) of ISO year.

IYYY

4-digit year based on the ISO standard.

J

Yes

Julian day; the number of days since January 1, 4712 BC. Number specified with J must be integers.

MI

Yes

Minute (0-59).

MM

Yes

Month (01-12; January = 01).

MON

Yes

Abbreviated name of month.

MONTH

Yes

Name of month.

PM
P.M.

Yes

Meridian indicator with or without periods.

Q

Quarter of year (1, 2, 3, 4; January - March = 1).

RM

Yes

Roman numeral month (I-XII; January = I).

RR

Yes

Lets you store 20th century dates in the 21st century using only two digits.

See Also: "The RR Datetime Format Element"

RRRR

Yes

Round year. Accepts either 4-digit or 2-digit input. If 2-digit, provides the same return as RR. If you do not want this functionality, then enter the 4-digit year.

SS

Yes

Second (0-59).

SSSSS

Yes

Seconds past midnight (0-86399).

TS

Yes

Returns a value in the short time format. Makes the appearance of the time components (hour, minutes, and so forth) depend on the NLS_TERRITORY and NLS_LANGUAGE initialization parameters.

Restriction: You can specify this format only with the DL or DS element, separated by white space.

TZD

Yes

Daylight saving information. The TZD value is an abbreviated time zone string with daylight saving information. It must correspond with the region specified in TZR. Valid in timestamp and interval formats, but not in DATE formats.

Example: PST (for US/Pacific standard time); PDT (for US/Pacific daylight time).

TZH

Yes

Time zone hour. (See TZM format element.) Valid in timestamp and interval formats, but not in DATE formats.

Example: 'HH:MI:SS.FFTZH:TZM'.

TZM

Yes

Time zone minute. (See TZH format element.) Valid in timestamp and interval formats, but not in DATE formats.

Example: 'HH:MI:SS.FFTZH:TZM'.

TZR

Yes

Time zone region information. The value must be one of the time zone region names supported in the database. Valid in timestamp and interval formats, but not in DATE formats.

Example: US/Pacific

WW

Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.

W

Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.

X

Yes

Local radix character.

Example: 'HH:MI:SSXFF'.

Y,YYY

Yes

Year with comma in this position.

YEAR
SYEAR

Year, spelled out; S prefixes BC dates with a minus sign (-).

YYYY
SYYYY

Yes

4-digit year; S prefixes BC dates with a minus sign.

YYY
YY
Y

Yes

Last 3, 2, or 1 digit(s) of year.

 我们来逐一测试下:

1、诸如- / , . ; : "text"  这类符号表达字符可以在结果中重现

SQL> select to_char(sysdate,'yyyy"year"mm-dd;') from dual;
TO_CHAR(SYSDATE,'YYYY"YEAR"MM-DD;')
------------------------------------------
2015year02-24;

2、年的显示,SYEAR/YEAR字符显示年,SYYYY/YYYY显示公元数值年,Y,YYY 以逗号分隔的年显示方式,IYYY/IYY/IY/I显示年后几位数值,RR/RRRR数值显示年, B.C.公元前, A.D.公元 SCC/CC 世纪,如果年的后两个数值是01~99,则世纪值=年的前两个值加一,否则与年前两个值相同。AM(A.M.)/PM(P.M.)显示上午还是下午

SQL> select to_char(sysdate,'SYEAR,YEAR') from dual;
TO_CHAR(SYSDATE,'SYEAR,YEAR')
--------------------------------------------------------------------------------
 TWENTY FIFTEEN,TWENTY FIFTEEN
SQL> select to_char(sysdate,'SYYYY,YYYY,Y,YYY,IYYY,IYY,IY,I:RR,RRRR:B.C.,A.D.,SCC,CC,AM,P.M.') from dual;
TO_CHAR(SYSDATE,'SYYYY,YYYY,Y,YYY,IYYY,IYY,IY,I:RR,RRRR:B.C.,A.D.,SCC,CC,AM,P.M.
--------------------------------------------------------------------------------
 2015,2015,2,015,2015,015,15,5:15,2015:A.D.,A.D., 21,21,PM,P.M.

3、月的显示, MM数值显示月份,MON字符显示月份,MON完整字符显示月份,RM罗马字母显示月份。

SQL> select to_char(sysdate,'MM,MON,MONTH,RM') from dual;
TO_CHAR(SYSDATE,'MM,MON,MONTH,RM')
--------------------------------------------------------------------------------
02,FEB,FEBRUARY ,II

4、天的显示,D  一周中的天数,DAY 天的名称,DD 月中的天数,DDD表示年中的天数,DY以缩写的星期显示。

SQL> select to_char(sysdate,'D,DAY,DD,DDD,DY') from dual;
TO_CHAR(SYSDATE,'D,DAY,DD,DDD,DY')
--------------------------------------------------------------------------------
3,TUESDAY  ,24,055,TUE

5、周显示,IW年中的周(1-53),WW年中的周以当年的第一天算第一周,W月中的周,以第每个月头一天算第一周。

SQL> select to_char(sysdate,'IW,WW,W') from dual;
TO_CHAR(SYSDATE,'IW,W
---------------------
09,08,4

6、时间格式 DL以长时间格式显示,DS以短时间格式显示,这两者取决于NLS_TERRITORY和NLS_LANGUAGE两个参数配置,不同地区显示的格式不一样。E,EE分别以缩写和全称的方式显示时代。

SQL> select to_char(sysdate,'DL:DS') from dual;
TO_CHAR(SYSDATE,'DL:DS')
--------------------------------------------------------------------------------
Tuesday, February 24, 2015:2/24/2015

7、小时 HH/HH12以12小时显示,HH24以24小时显示。

SQL> select to_char(SYSTIMESTAMP,'HH,HH12,HH24') from dual;
TO_CHAR(SYSTIMESTAMP,'HH
------------------------
01,01,13

8、分钟MI

SQL> select to_char(systimestamp,'MI') from dual;
TO_CHA
------
13

9、秒数 SS显示秒数,FF显示毫秒精确度[1~9],FM让返回的值没有前后空格,FX精确匹配日期格式类型。

SQL> select to_char(SYSTIMESTAMP,'SS.FF3,SSSSS') from dual;
TO_CHAR(SYSTIMESTAMP,'SS.FF3,SSSSS')
------------------------------------------------------
58.180,49438

10、其他 TS短格式显示时间,TZD白天即使,TZH区域小时,TZM区域分钟,TZR时间区域信息,X小数点,Q季度。

SQL> select to_char(systimestamp,'TS,TZD,TZH,TZM,TZR') from dual;
TO_CHAR(SYSTIMESTAMP,'TS,TZD,TZH,TZM,TZR')
--------------------------------------------------------------------------------
2:09:27 PM,,+08,00,+08:00
SQL> select to_char(systimestamp,'XQ') from dual;
TO_CHA
------
.1

11、计算时间差

年份差

select floor(to_number(sysdate-to_date('01-31-1999','MM-DD-YYYY'))/365) as "YEARS" FROM DUAL;

月份差

select months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL; 
select ceil(months_between(to_date('01-31-1999','MM-DD-YYYY'),to_date('12-31-1998','MM-DD-YYYY')))"MONTHS" FROM DUAL;

天数差(时*24,分24*60,以此类推)

select floor(to_number(sysdate-to_date('01-31-1999','MM-DD-YYYY'))) as "YEARS" FROM DUAL;

 

通过EXTRACT()获取精准的时间

SQL> SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 2:38:40') from dual;
EXTRACT(MINUTEFROMTIMESTAMP'2001-02-162:38:40')
-----------------------------------------------
                                             38

通过NEXT_DAY()函数获取未来时间,这里1-7分别表示从日、一、二、三、四、五、六

SQL> select sysdate,next_day(sysdate,6) from dual;
SYSDATE             NEXT_DAY(SYSDATE,6)
------------------- -------------------
2015-03-09 12:44:02 2015-03-13 12:44:02

查看有关时间的环境参数设置:

SQL> col PARAMETER for a30
SQL> col VALUE for a40
SQL> select * from nls_session_parameters;
PARAMETER                      VALUE
------------------------------ ----------------------------------------
NLS_LANGUAGE                   AMERICAN
NLS_TERRITORY                  AMERICA
NLS_CURRENCY                   $
NLS_ISO_CURRENCY               AMERICA
NLS_NUMERIC_CHARACTERS         .,
NLS_CALENDAR                   GREGoRIAN
NLS_DATE_FORMAT                YYYY-MM-DD HH24:MI:SS
NLS_DATE_LANGUAGE              AMERICAN
NLS_SORT                       BINARY
NLS_TIME_FORMAT                HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT           DD-MON-RR HH.MI.SSXFF AM
PARAMETER                      VALUE
------------------------------ ----------------------------------------
NLS_TIME_TZ_FORMAT             HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT        DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY              $
NLS_COMP                       BINARY
NLS_LENGTH_SEMANTICS           BYTE
NLS_NCHAR_CONV_EXCP            FALSE

 

您可能感兴趣的文档:

--结束END--

本文标题: DATETIME FORMATE(时间格式)

本文链接: https://www.lsjlt.com/news/44587.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • DATETIME FORMATE(时间格式)
    来源于Oracle官方文档,为了工作中方便使用,这里截取下来供参考:http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements004...
    99+
    2022-10-18
  • python,datetime 时间格
    import datetime,re def parse_date(date_str): try: if not date_str: return None if "-" in...
    99+
    2023-01-31
    时间 python datetime
  • python常用时间库time、datetime与时间格式之间如何转换
    这篇“python常用时间库time、datetime与时间格式之间如何转换”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“p...
    99+
    2023-07-05
  • pandas时间序列之如何将int转换成datetime格式
    目录将int转换成datetime格式原始时间格式pandas 时间数据处理转化时间类型生成时间序列提取时间属性计算时间间隔计算时间推移获取当前机器的支持的最大时间和最小时间将int...
    99+
    2022-11-11
  • javascript 时间戳转datetime
    JavaScript 是一门强大的脚本语言,它广泛应用于网页前端开发和服务器端编程。在 JavaScript 中,时间戳和日期时间之间的转换是十分常见的操作。时间戳是指自 1970 年 1 月 1 日 00:00:00 UTC(协调世界时)...
    99+
    2023-05-15
  • MySQL 的 datetime等日期和时间处理SQL函数及格式化显示
    MySQL 的 datetime等日期和时间处理SQL函数及格式化显示 MySQL 时间相关的SQL函数: MySQL的SQL DATE_FORMAT函数: 用于以不同的格式显示日期/时间数据。 DATE_FORMAT(date...
    99+
    2023-09-16
    mysql 数据库 java Powered by 金山文档
  • javascript转时间格式
    在开发中,时间格式的转换是比较常见的操作。其中,JavaScript提供了多种方法来转换时间格式,本文将介绍几种常见的转换方式。一、时间戳转日期时间戳是指自1970年1月1日00:00:00以来的秒数,JavaScript提供了将时间戳转成...
    99+
    2023-05-16
  • Python格式化时间
      walker经常用到当前时间和相对时间,用来统计程序执行的效率,简单记一下,便于copy。>>> import time #相对时间 >>> startTime = time.time() >...
    99+
    2023-01-31
    时间 Python
  • MySQL 格式化时间
    MySQL如何正确格式化时间 MySQL是一个非常流行的关系型数据库管理系统,它提供了一种使用SQL语言来管理和操作数据库的方法。在MySQL中,时间格式化是一个常见的需求,但很多人可能并不了解如何正确格式化时间。在本文中,我们将介绍MyS...
    99+
    2023-08-16
    mysql 数据库
  • Java——时间戳和时间格式转换
    时间戳(TimeStamp):通常是一个字符序列,唯一地标志某一刻的时间。Java 中时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数。 一、获取时间戳的方式 1.System.currentTimeM...
    99+
    2023-08-17
    java
  • MySQL datetime类型与时间、日期格式字符串大小比较的方法
    目录一、前提1、mysql版本信息:2、表字段:二、使用>、<比较1.  日期格式比较:2. 时间格式三、between and1. 日期格式四、总结一、前提 1、MySQL版本信息: MySQL版...
    99+
    2022-11-14
  • Python 时间操作datetime详情
    目录1. datetime 模块概述2. datetime 组成部分3. datetime 常量4. datetime 常用方法5、实操6、总结复习回顾: Python 在对时间操作...
    99+
    2022-11-12
  • hadoop时间格式转换
    在Hadoop中,时间格式转换可以使用Java的SimpleDateFormat类来实现。下面是一个示例代码,将一个时间字符串从"y...
    99+
    2023-10-11
    hadoop
  • DateTime存储格式是什么
    DateTime存储格式是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。  什么是DateTime存储格式  在讲解DateTime存...
    99+
    2022-10-18
  • php时间戳如何转换时间格式
    这篇文章主要介绍“php时间戳如何转换时间格式”,在日常操作中,相信很多人在php时间戳如何转换时间格式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php时间戳如何转换时间格式”的疑惑有所帮助!接下来,请跟...
    99+
    2023-07-05
  • 【Java-时间格式解析】Java解析形如 yyyy-MM-ddTHH:mm:ssZ 的时间格式
    【Java-时间格式解析】Java解析形如 yyyy-MM-ddTHH:mm:ssZ 的时间格式 1)yyyy-MM-ddTHH:mm:ss.SSSZ 时间格式解释2)使用Java 对 yyyy-MM-ddTHH:mm:ss.SSS...
    99+
    2023-08-19
    java 开发语言
  • Python时间模块之datetime模块
    目录 简介 函数介绍及运用 date:日期类 1.获取当前时间  2.日期对象的属性 3.date类中时间和时间戳的转换: 4.修改日期使用replace方法  time:时间类  time类操作 datetime:日期时间类 timede...
    99+
    2023-09-12
    python datetime python 日期时间
  • Python时间操作datetime详情(下)
    目录1. datetime.date 类1.1datetime.date 类格式1.2datetime.date类方法和属性2. datetime.time 类2.1datetime...
    99+
    2022-11-12
  • MySql表分区(根据时间datetime)
    timestamp 类型分区请移步=>MySql表分区(根据时间timestamp)环境:  MySql8.0.18(5.6和5.7的未验证)  分区条件的字段类型是datetime完整的sql操作...
    99+
    2022-10-18
  • Python的时间模块datetime详解
    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime.da...
    99+
    2022-06-04
    详解 模块 时间
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作