Home> Database> Oracle> body text

How to convert string to date in oracle

DDD
Release: 2023-08-22 14:48:44
Original
9131 people have browsed it

Oracle string date conversion can use the "TO_DATE" function. Use the syntax "TO_DATE(char, format)" of the TO_DATE function to complete the conversion. Among them, char represents the string to be converted into a date, and format represents The date format of the string. It should be noted that if the string does not match the specified date format, the TO_DATE function will throw an error. Therefore, make sure to specify the correct date format when using the TO_DATE function for date conversion.

How to convert string to date in oracle

#The operating environment of this article: Windows 10 system, Oracle 19c, Dell G3 computer.

In Oracle database, you can use the TO_DATE function to convert a string into a date.

The syntax of the TO_DATE function is as follows:

TO_DATE(char, format)
Copy after login

Among them, char represents the string to be converted to date, and format represents the date format of the string.

The following are some commonly used date formats and their meanings:

YYYY-MM-DD: year-month-day, for example: '2022-01-01'

YYYY/MM/DD: year/month/day, for example: '2022/01/01'

DD-MON-YYYY: day-month-year, for example: '01-JAN-2022'

DD/MM/YYYY: day/month/year, for example: '01/01/2022'

MON-DD-YYYY: month-day-year, for example: 'JAN-01- 2022'

MON/DD/YYYY: month/day/year, for example: 'JAN/01/2022'

DD-MON-YYYY HH24:MI:SS: date and time, For example: '01-JAN-2022 12:00:00'

Here are some examples showing how to use the TO_DATE function to convert a string to a date:

Convert the string '2022-01 -01' to date:

SELECT TO_DATE('2022-01-01', 'YYYY-MM-DD') FROM DUAL;
Copy after login

Convert string '01-JAN-2022' to date:

SELECT TO_DATE('01-JAN-2022', 'DD-MON-YYYY') FROM DUAL;
Copy after login

Convert string '01-JAN-2022 12:00:00' Convert to date and time:

SELECT TO_DATE('01-JAN-2022 12:00:00', 'DD-MON-YYYY HH24:MI:SS') FROM DUAL;
Copy after login

It should be noted that if the string does not match the specified date format, the TO_DATE function will throw an error. Therefore, when using the TO_DATE function for date conversion, make sure to specify the correct date format to avoid errors.

In addition, you can also use the TO_TIMESTAMP function to convert a string into a date of timestamp type. The syntax and usage of the TO_TIMESTAMP function are similar to the TO_DATE function, except that the returned result is a timestamp type value.

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!