Home >Database >Oracle >How to convert time date into timestamp in oracle

How to convert time date into timestamp in oracle

青灯夜游
青灯夜游Original
2022-03-25 17:20:0923916browse

Conversion method: 1. Use the to_char() function to output the time and date in the specified format. The result is a string, the syntax is "to_char(date,"conversion format")"; 2. Use to_date () function, syntax "to_date("time string","converted time and date format")".

How to convert time date into timestamp in oracle

The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.

oracle time and date conversion into timestamp

In Oracle, there are two methods to convert time and date into timestamp:

  • to_char() function

  • to_date() function

##1. to_char()

Output the time and date in the specified format, and the result is a string, not a date type.

to_char(日期,"转换格式" )

will convert the given date according to the "conversion format".

Example:

select to_char(sysdate, 'yyyy-mm-dd') from dual;
select to_char(sysdate, 'yyyy/mm/dd') from dual;
select to_char(sysdate, 'yyyymmdd') from dual;
select to_char(sysdate, 'yyyymmdd hh24:mi:ss') from dual;

You can also use to_char() to get a separate string of year, month, day, hour, minute and second

select to_char(sysdate,'yyyy') from dual;
select to_char(sysdate,'mm') from dual;
select to_char(sysdate,'hh24') from dual;
select to_char(sysdate,'mi') from dual;

2, to_date()

Convert the string to the specified time and date format

to_date("时间字符串","转换的时间日期格式")

The format of the two parameters must match, otherwise an error will be reported.

That is, the first parameter is interpreted according to the format of the second parameter.

Example:

select to_date('20220103','yyyymmdd') from dual;
select to_date('20220103','yyyy-mm-dd') from dual;
select to_date('20220103','yyyy/mm/dd') from dual;
select to_date('20220103','yyyy-mm-dd hh24:mi:ss') from dual;

Recommended tutorial: "

Oracle Tutorial"

The above is the detailed content of How to convert time date into timestamp in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn