To extract the hour, minute, second from a time stamp, you pass the corresponding value hour, minute and second to the DATE_PART() function: SELECT date_part( 'hour', TIMESTAMP ' 10:20:30') h,ĭate_part( 'minute', TIMESTAMP ' 10:20:30') m,ĭate_part( 'second', TIMESTAMP ' 10:20:30') s To extract the day part from a time stamp, you pass the day value to the DATE_PART() function: SELECT date_part( 'day', TIMESTAMP ' 10:20:30') To get the current millennium, you use the DATE_PART() function with the NOW() function as follows: SELECT date_part( 'millennium', now()) To extract the week number from a time stamp, you pass the week as the first argument: SELECT date_part( 'week', TIMESTAMP '') To get the decade from a time stamp, you use the statement below: SELECT date_part( 'decade', TIMESTAMP '') To get the month, you pass the month to the DATE_PART() function: SELECT date_part( 'month', TIMESTAMP '') ![]() To extract the quarter, you use the following statement: SELECT date_part( 'quarter', TIMESTAMP '') To extract the year from the same timestamp, you pass the year to the field argument: SELECT date_part( 'year', TIMESTAMP '') ![]() The following example extracts the century from a time stamp: SELECT date_part( 'century', TIMESTAMP '') The DATE_PART() function returns a value whose type is double precision. If the source evaluates to DATE, the function will cast to TIMESTAMP. The source is a temporal expression that evaluates to TIMESTAMP, TIME, or INTERVAL. The values of the field must be in a list of permitted values mentioned below: The field is an identifier that determines what field to extract from the source. ![]() The following illustrates the DATE_PART() function: DATE_PART(field,source)Ĭode language: SQL (Structured Query Language) ( sql ) The DATE_PART() function extracts a subfield from a date or time value. Summary: in this tutorial, we will introduce you to the PostgreSQL DATE_PART() function that allows you to retrieve subfields e.g., year, month, week from a date or time value. Introduction to the PostgreSQL DATE_PART function
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |