In the example, we have tried to illustrate how to calculate the date and time after a specified interval based on the current date and time. Example #4įind the date and time after an interval of 1 year based on the current timestamp. We have illustrated the use of DATE_SUB() to get the date and time of the past 1 hour, based on the current date and time, i.e. This example is primarily an extension of the previous example. You may play around with the DATE_SUB() function to get values in the other intervals such as a month, hour, minute, second, year, etc.įind the date and time in the past 1 hour based on the current timestamp. The function subtracts the 30 days from the current date and time. In this case, the argument date is the output of the NOW() function, and the interval is 30 days. The function basically subtracts a time value ( given as interval) from the argument date. We have used a system-defined function DATE_SUB(arg, interval). In this example, we have tried to illustrate how to calculate the date and time of the past durations based on the current date and time settings. Example #2įind the date and time in the past 30 days based on the current timestamp. The output is completely system dependent and hence may vary from one system to another. Whether we receive a numeric or string output depends upon the format specified in the system settings. In this example, you can clearly see that the NOW() function returns the current system date and time. SQL query to illustrate the functionality of the NOW() function in MySQL. Given below are the examples of SQL now: Example #1 Having discussed the basic syntax and parameters used for working with the NOW() function, let us go ahead and discuss a few examples to understand the functionality in great detail. The NOW() function can be used as a part of the SELECT statement for fetching the current date and time based on the system settings, WHERE OR HAVING clause to filter based on the current time in the filtering expression, INSERT statement for data entering the current date and time for auditing or future references, etc. Return type: The function returns a string/numeric value in the ‘YYYY-MM-DD HH:MM:DD’ (string) or ‘YYYYMMDDHHMMSS.uuuu’ (numeric) format. The function does not require any user-defined arguments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |