Sunday, December 28, 2014

Displaying the Greeting Message based on Time in MySQL


Hi, My requirement is based on Current time i want to display Greeting message.

For example, if time is less than 12PM then i need to display "Morning" and if time is less than 5PM then i need to display "Afternoon" and if time is above 5 PM i need to display "Evening".

Step1: Selecting the Current time.
SELECT now() from DUAL;
Output:
 '2014-12-28 17:35:46'

Step2: In the Current time getting the hours.
SELECT TIME_FORMAT(now(),'%H') from DUAL;
Output: 
17

Step3: Now based on hours we need to display greeting message. So we need to use if condition.
SELECT IF((SELECT TIME_FORMAT(now(),'%H') from DUAL) < 12,'Morning', IF((SELECT TIME_FORMAT(now(), 
'%H') from DUAL) < 17,'Afternoon','Evening'));
Output:
'Evening'

Happy coding!

0 comments: