Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

MySQL Interview Questions and Answers

Ques 81. How To Convert Character Strings to Dates?

Ans. If you have a character string that represents a date, and you want to convert it into a date value, you can use the STR_TO_DATE(string, format) function. STR_TO_DATE() shares the same formatting codes with DATE_FORMAT() function. The tutorial exercise below shows you some good examples:

SELECT STR_TO_DATE('Friday, January 31, 1997', '%W, %M %e, %Y') FROM DUAL;
1997-01-31

SELECT STR_TO_DATE('Friday, January 31, 1997, 09:26:50 AM', '%W, %M %e, %Y, %h:%i:%s %p') FROM DUAL;
1997-01-31 09:26:50

SELECT STR_TO_DATE('31-Jan-1997 09:26:50.000123', '%d-%b-%Y %H:%i:%s.%f') FROM DUAL;
1997-01-31 09:26:50.000123

Is it helpful? Add Comment View Comments
Ques 82. What Are Date and Time Intervals?
Ans. A date and time interval is special value to be used to increment or decrement a date or a time at a given date or time unit. A data and time interval should be expression in the format of "INTERVAL expression unit", where "unit" and "expression" should follow these rules:

unit - expression value format

MICROSECOND - MICROSECONDS
SECOND - SECONDS
MINUTE - MINUTES
HOUR - HOURS
DAY - DAYS
WEEK - WEEKS
MONTH - MONTHS
QUARTER - QUARTERS
YEAR -YEARS
SECOND_MICROSECOND - 'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND - 'MINUTES.MICROSECONDS'
MINUTE_SECOND - 'MINUTES:SECONDS'
HOUR_MICROSECOND - 'HOURS.MICROSECONDS'
HOUR_SECOND - 'HOURS:MINUTES:SECONDS'
HOUR_MINUTE - 'HOURS:MINUTES'
DAY_MICROSECOND - 'DAYS.MICROSECONDS'
DAY_SECOND - 'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE - 'DAYS HOURS:MINUTES'
DAY_HOUR - 'DAYS HOURS'
YEAR_MONTH - 'YEARS-MONTHS'
Is it helpful? Add Comment View Comments
Ques 83. How To Increment Dates by 1 in MySQL?
Ans. If you have a date, and you want to increment it by 1 day, you can use the DATE_ADD(date, INTERVAL 1 DAY) function. You can also use the date interval add operation as "date + INTERVAL 1 DAY". The tutorial exercise below gives you some good examples:

SELECT DATE_ADD(DATE('1997-01-31'), INTERVAL 1 DAY) FROM DUAL;
1997-02-01

SELECT DATE('1997-01-31') + INTERVAL 1 DAY FROM DUAL;
1997-02-01
Is it helpful? Add Comment View Comments
Ques 84. How To Decrement Dates by 1 in MySQL?
Ans. If you have a date, and you want to decrement it by 1 day, you can use the DATE_SUB(date, INTERVAL 1 DAY) function. You can also use the date interval subtraction operation as "date - INTERVAL 1 DAY". The tutorial exercise below gives you some good examples:

SELECT DATE_SUB(DATE('1997-03-01'), INTERVAL 1 DAY) FROM DUAL;
1997-02-28

SELECT DATE('1997-01-31') - INTERVAL 1 DAY FROM DUAL;
1997-02-28
Is it helpful? Add Comment View Comments
Ques 85. How To Calculate the Difference between Two Dates?
Ans. If you have two dates, and you want to know how many days between them, you can use the DATEDIFF(date1, date2) function as shown below:

SELECT DATEDIFF(DATE('1997-02-28'), DATE('1997-03-01')) FROM DUAL;
-1
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2020 WithoutBook