8.5.4. Interval Input

8.5.4. Interval Input
8.5.4.Interval input
interval values can be written using the following verbose syntax:
interval values ​​detailed syntax of input:
 
[@] quantity unit [quantity unit...] [direction]
 
where quantity is a number (possibly signed); unit is microsecond , millisecond , second , minute , hour , day , week , month , year , decade , century , millennium , or abbreviations  or plurals of these units; direction can be ago or empty. The at sign ( @ ) is optional noise. The amounts of the different units are implicitly added with appropriate sign accounting. ago negates  all the fields. This syntax is also used for interval output, if IntervalStyle is set to postgres_verbose .
quantity is a number (which may be signed); unit is microsecond , millisecond , SECOND , minute , hour , Day , Week , month The , year , Decade , CENTURY , Millennium , or the unit abbreviations or complex; direction can be ago or empty . The @ symbol is optional. Different unit implicitly add the appropriate symbols. ago will give you all the unit minus sign. If IntervalStyle set postgres_verbose, then this syntax can also be used for interval output.
 
Quantities of days, hours, minutes, and seconds can be specified without explicit unit markings. For  example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10 sec' . Also, a combination of years and months can be specified with a dash; for example '200-10' is read the  same as '200 years 10 months' . (These shorter forms are in fact the only ones allowed by the  SQL standard, and are used for output when IntervalStyle is set to sql_standard .)
Days, hours, minutes, and seconds using the unit can not be explicitly defined. For example, "12:59:10 1 'and ' 1 day 12 hours 59 min 10 sec ' same. Of course, mixing can be used year and month - specified number, such as "" '200-10' and '200 years 10 months' same. (In fact, more abbreviated format of the SQL standard only permitted when IntervalStyle to use sql_standard. )
 
Interval values can also be written as ISO 8601 time intervals, using either the “format with designators”  of the standard's section 4.4.3.2 or the “alternative format” of section 4.4.3.3. The format with  designators looks like this:
It may be written as a value of interval time interval value to ISO 8601, Section 4.4.3.2 using (this section is not found) "Format with indicator" or Section 4.4.3.3 (not found in this section) "special belt format". Format with indicators that looks like this:
 
P quantity unit [ quantity unit ...] [ T [ quantity unit ...]]
 
The string must start with a P , and may include a T that introduces the time-of-day units. The available  unit abbreviations are given in Table 8.16. Units may be omitted, and may be specified in any order, but units smaller than a day must appear after T . In particular, the meaning of M depends on whether  it is before or after T .
The string must begin with P, and the unit may comprise a pull-in time T. Table 8.16 gives the abbreviation for available units. Units can be omitted, can also be specified in any order, but less than a day unit T must appear later. In particular, the meaning of M depends on whether it is before or after T.
 
In the alternative format:
Another format:
 
P [ years-months-days ] [ T hours:minutes:seconds ]
the string must begin with P , and a T separates the date and time parts of the interval. The values are  given as numbers similar to ISO 8601 dates.
P No string must be opened in order to distinguish T interval of time and date portion. ISO 8601 is similar to the digital value given.
 
When writing an interval constant with a fields specification, or when assigning a string to an interval  column that was defined with a fields specification, the interpretation of unmarked quantities  depends on the fields . For example INTERVAL '1' YEAR is read as 1 year, whereas INTERVAL  '1' means 1 second. Also, field values “to the right” of the least significant field allowed by the  fields specification are silently discarded. For example, writing INTERVAL '1 day 2:03:04'  HOUR TO MINUTE results in dropping the seconds field, but not the day field.
When using field interval constant written instructions, or the string is assigned to the field using the column described interval defined for interpretation depends on the amount of unlabeled field. For example, INTERVAL'1'YEAR read as 1 year, and INTERVAL'1 'represents one second. Similarly, the least significant specification allows Field "right side" of the field value are ignored. For example, INTERVAL'1 day 2: 03: 04'HOUR TO MINUTE field will result in the deletion seconds, rather than days field.
 
According to the SQL standard all fields of an interval value must have the same sign, so a leading  negative sign applies to all fields; for example the negative sign in the interval literal '-1 2:03:04'  applies to both the days and hour/minute/second parts. PostgreSQL allows the fields to have different  signs, and traditionally treats each field in the textual representation as independently signed, so that  the hour/minute/second part is considered positive in this example. If IntervalStyle is set to  sql_standard then a leading sign is considered to apply to all fields (but only if no additional  signs appear). Otherwise the traditional PostgreSQL interpretation is used. To avoid ambiguity, it's  recommended to attach an explicit sign to each field if any field is negative.
According to the SQL standard, all field interval value must have the same sign, thus leading minus sign for all fields; e.g., Text interval "-1 2:03:04" minus sign applies to the date and hour / minute / second section. PostgreSQL allows fields have different signs, and traditionally the text representation of each field as independent, and therefore in this example, hour / minute / second portion is considered positive. If IntervalStyle set sql_standard, it is considered the leading symbol for all fields (but only if no other symbol appears). Otherwise, the traditional PostgreSQL interpretation. To avoid ambiguity, if any field is negative, an additional explicit recommendations symbols on each field.
 
In the verbose input format, and in some fields of the more compact input formats, field values can  have fractional parts; for example '1.5 week' or '01:02:03.45' . Such input is converted to the  appropriate  number of months, days, and seconds for storage. When this would result in a fractional  number of months or days, the fraction is added to the lower-order fields using the conversion factors  1 month = 30 days and 1 day = 24 hours. For example, '1.5 month' becomes 1 month and 15  days. Only seconds will ever be shown as fractional on output.
In the detailed input format, and in some more compact format of the input field, the field may contain a decimal part value; for example, "1.5 Week" or "01: 02: 03.45." Such input will be converted to the appropriate number of months, number of days and number of seconds for storage. If it results in a few days or months decimal, the score will then 1 month = 30 days and 1 day = 24 hours a conversion factor added to the low-order field. For example, "1.5 months' to one month 15 days. In the output, only the second will be displayed as a decimal.
 
Table 8.17 shows some examples of valid interval input.
Table 8.17 for the interval input few examples:
Internally interval values are stored as months, days, and seconds. This is done because the number  of days in a month varies, and a day can have 23 or 25 hours if a daylight savings time adjustment is  involved. The months and days fields are integers while the seconds field can store fractions. Because  intervals are usually created from constant strings or timestamp subtraction, this storage method  works well in most cases, but can cause unexpected results:
Interval values ​​are stored in the internal months, days and seconds. This is because of the different number of days in a month, and if the adjustment when it comes to daylight saving time, then one day there are 23 or 25 hours. Month and day fields are integers, while the seconds field can store scores. As the interval is usually created by the constant strings or timestamp subtraction, this storage method can work well in most cases, but can lead to unexpected results:
 
SELECT EXTRACT(hours from '80 minutes'::interval);
date_part
-----------
1
SELECT EXTRACT(days from '80 hours'::interval);
date_part
-----------
0
Functions justify_days and justify_hours are available for adjusting days and hours that  overflow their normal ranges.
Justify_days justify_hours function and can be used to adjust the number of days and hours beyond the normal range.
 
Published 341 original articles · won praise 54 · views 880 000 +

Guess you like

Origin blog.csdn.net/ghostliming/article/details/104643967