![]() But if we use it with the + operator, then it will be subtracted from the input date/time. If we use a negative value with the - operator, then the specified number of hours will be added to the input date/time. It’s possible to perform date arithmetic with negative values. If the hours to be subtracted are in 24 hour increments, we can alternatively use days: SELECT timestamp ' 15:45' - interval '1 day' Converting a timestamp to a date loses any fractional seconds or time zone details in the timestamp. You are now just comparing how many days in minutes were between the two values, instead of comparing the actual minutes. Note: This function equals the CURTIME () function. I have a time field in my database, it has the value 22:05. Note: The time is returned as 'HH-MM-SS' (string) or as HHMMSS.uuuuuu (numeric). We can alternatively subtract the equivalent number in minutes: SELECT time '15:45' - interval '120 minutes' Probably because by converting this to a date you lose out on minute granularity. SELECT FROM logins WHERE logindate > DATESUB ( now (), interval 45 minute ) As you can see, this would retrieve all records from the logins table with a login date greater than the current time minus 45 minutes, or in other words, the past 45 minutes. The CURRENTTIME () function returns the current time. ![]() We can also add a date and time value together, and subtract hours from that: SELECT date '' + time '03:00' - interval '2 hours' We can even subtract hours from a date value: SELECT date '' - interval '8 hours' ![]() We can also subtract hours from an interval: SELECT interval '5 hours' - interval '2 hours' So to subtract one or more hours, we can use hour or hours: SELECT time '07:00' - interval '1 hour' Īnd in plural form: SELECT time '07:00' - interval '2 hours' Īnd here it is with a timestamp value: SELECT timestamp ' 09:00' - interval '30 hours' We can specify intervals when performing arithmetic against dates and times. We can also subtract hours from a date value or a date and time combination. In PostgreSQL, we can use the - operator to subtract one or more hours from a time value.īy “time” value, this could be an actual time value, a timestamp, or an interval. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |