Oracle Date format conversion, ToChar (), ToDate ()

Source: Internet
Author: User

Oracle Date format Conversion

This article mainly describes date conversions in Oracle.

1. Date converted to string (as of October 20, 2016)

Select To_char (sysdate, ' yyyy-mm-dd hh24:mi:ss ') Strdatetime from dual; --Get year-month-day time: minutes: seconds
--Display the result: 2016-10-20 12:35:21

Select To_char (sysdate, ' yyyymmdd hh24:mi:ss ') Strdatetime from dual; --Acquisition date: minutes: Seconds
-Display Result: 20161020 13:39:25

Select To_char (sysdate, ' YYYYMMDD ') strdatetime from dual; --Date of acquisition
--Display Result: 20161020

Select To_char (sysdate, ' yyyy ') stryear from dual; --Get year
--Display Result: 2016

Select To_char (sysdate, ' mm ') Strmonth from dual; --Get the month
--Display Result: 10

Select To_char (sysdate, ' DD ') strday from dual; --Acquisition Day
--Display Result: 20

Select To_char (sysdate, ' hh24 ') Strhour from dual; --When acquired
--Display Result: 13

Select To_char (sysdate, ' mi ') Strminute from dual; --Get points
--Display Result: 46

Select To_char (sysdate, ' SS ') Strsecond from dual; --Get seconds
--Display Result: 43

2. String and Time transfer

Select To_date (' 2010-10-20 13:23:44 ', ' yyyy-mm-dd hh24:mi:ss ') DateTime from dual;
Display results: 2010/10/20 13:23:44

Select To_date (' 2010-10-20 13:23:44 ', ' yyyy/mm/dd hh24:mi:ss ') DateTime from dual;

Display results: 2010/10/20 13:23:44

Select To_char (to_date (222, ' J '), ' JSP ') from dual;

Displaying results: Hundred Twenty-two

If you follow the example below, you will get an error: ORA-01849: The hour value must be between 1 and 12. (Because the HH is 12 binary, no 13 error)

Select To_date (' 2005-12-25,13:25:59 ', ' Yyyy-mm-dd,hh:mi:ss ') from dual;

3. Query the day of the week

Select To_char (to_date (' 2012-10-20 ', ' yyyy-mm-dd '), ' Day ') strday from dual;
displaying results: Saturday

Select To_char (to_date (' 2012-10-20 ', ' yyyy-mm-dd '), ' Day ', ' nls_date_language = 中文版 ') strday from dual;
displaying results: Saturday

4. Number of days between two dates

Select Floor (sysdate-to_date (' 20161010 ', ' YYYYMMDD ')) Strtime from dual;

--Where SYSDATE=2016/10/20 17:10:51
--Display Results: 10

5. The use of time null

Select To_date (null) from dual;

6. Month Difference

Select Months_between (Date ' 2014-04-23 ', Date ' 2013-04-23 ') days from dual;

This article from: http://www.jb51.net/article/45591.htm

Http://www.cnblogs.com/xiaobaihome/archive/2012/03/14/2395568.html

Oracle Date format conversion, ToChar (), ToDate ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.