December Type
November 6, 2013 in News Tags: Courses, education
We will gradually respond to the most frequently asked questions. How to get the difference between the two variables of type Time in the form of variable Time? Get the interval between the two values of type Time in the following way. First, subtract the value of one another, and then add the result to the "midnight". For example, to find out how much time is left until specific event, to query select cast ('0: 00 'as time) + (cast ('17: 00' as time)-current_time) from Table1 How to get the difference between the two variables of type Timestamp in the form of a variable of type Time? To get the interval between the two variables of type Timestamp should be subtracted from the first value of the second, then multiply the result by the number of seconds in a day and add the result to midnight. The following example shows how much time remains before the fireworks (assuming that today May 9). select cast ('0: 00 'as time) + (cast ('5 / 9 / 02 21:00' as timestamp) – current_timestamp) * 24 * 60 * 60 from table1 How to get the difference of type Timestamp between two dates, as specified Timestamp? To solve this problem, we must first value of type Timestamp subtract the second and add the result to date, which shall be considered a starting point.
Take, for example, for zero on 31 December 1999 and calculate the travel time to train Moscow – Vladivostok, which departs from the station to Moscow Yaroslavl October 11, 2008 at 01:01 and arrive in Beijing in October 19th, 2008 at 13:47. As a result of the following query select cast ('12 / 31/99 'as timestamp) + (cast ('10 / 19/08 13:47' as timestamp) – cast ('10 / 11/08 1:01 'as timestamp)) from table1 obtain the result of 8 days, 12 hours and 46 minutes (zero of 31 December 1999, in this case). How to subtract or add the variable of Time to a date specified as a variable of type Timestamp? That to solve this problem, we must subtract "Midnight" from the time of type Time, and the result is then subtracted or added to a date specified as a Timestamp. The following example calculates how much time is 2 hours. select current_timestamp + (cast ('2: 00 'as time) – cast ('0: 00' as time)) / (60 * 60 * 24) from table1; As take away or add part of the day to a variable of type Timestamp? You simply add or subtract from a variable of type Timestamp number of seconds.
This example shows what will be the date and time of 2 hours. select current_timestamp + (2.0 / 24.0) from table1 As subtract and add combination of date-time? When the date and time are added, the result appears as a type of Timestamp and then can work with it as usual. This example shows how date and time were yesterday. select current_date + current_time – 1 from table1; How do I calculate the average value or the sum of the column containing the time? First we need to create a field of type Time, containing the number of seconds since midnight, and then calculate the average or the sum of the column and add the result obtained by midnight. select cast ('0: 00 'as time) + avg (seconds) from times