How To Write Date and Time Literals?

Answer

MySQL offers a number of formats for you to use to enter date and time literals:

► ANSI standard format: "YYYY-MM-DD HH:MM:SS".
► Non-standard limiters. Like: "YYYY/MM/DD HH^MM^SS" or "YYYY.MM.DD HH-MM-SS".
► No limiters. Like: "YYYYMMDD" for a date or "HHMMSS" for a time.
► Decimal numbers. Like: 8 digits dddddddd for a date or 6 digits dddddd for a time.

The tutorial exercise below gives you some good examples:

SELECT DATE('1997-01-31') FROM DUAL;
1997-01-31

SELECT DATE('1997-01-31 09:26:50') FROM DUAL;
1997-01-31

SELECT TIME('1997-01-31 09:26:50') FROM DUAL;
09:26:50

SELECT DATE('1997/01/31 09^26^50') FROM DUAL;
1997-01-31

SELECT TIME('1997/01/31 09^26^50') FROM DUAL;
09:26:50

SELECT DATE('19970131') FROM DUAL;
1997-01-31

SELECT TIME('092650') FROM DUAL;
09:26:50

SELECT DATE(19970131) FROM DUAL; -- Crazy format
1997-01-31

SELECT TIME(092650) FROM DUAL; -- Crazy format
09:26:50

Is it helpful? Add Comment View Comments
Ques 79. How To Enter Microseconds in SQL Statements?
Ans. If you want to enter microseconds in a SQL statements, you can enter them right after the time string as a 6-digit number delimited with '.'. '0' will be padded to right if not enough digits. Here are some good examples:

SELECT TIME('1997/01/31 09^26^50.123') FROM DUAL;
09:26:50.123000

SELECT TIME('1997/01/31 09^26^50.000123') FROM DUAL;
09:26:50.000123

All sql dba Questions

Ask your interview questions on sql-dba

Write Your comment or Questions if you want the answers on sql-dba from sql-dba Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---