Method 4: Using Scope Resolution “::” OperatorĪlternatively, the scope resolution operator can be used to convert a specific timestamp into a date. The TO_CHAR() function will convert the given timestamp to the specified date format. The “YYYY-MM-DD” format is passed as an argument to the TO_CHAR() function. The NOW() function returns today’s DateTime. The NOW() function is passed as an argument to the TO_CHAR() function. Here is an example: SELECT TO_CHAR(NOW(), 'YYYY-MM-DD') As Current_Date The TO_CHAR() function assists us in converting the given timestamp into a date using a specific format. Method 3: Using Postgres TO_CHAR() Function “AS DATE” represents that the given timestamp should be converted into a date. A timestamp is passed as an argument to the CAST operator. Similarly, a timestamp can be converted to a date using the CAST operator, as shown in the following code snippet: SELECT CAST(' 20:41:12.791354-08' AS DATE) The CAST operator in Postgres allows us to convert one data type to another. The below snippet shows the “current timestamp” and the “current timestamp converted to date”: Passing the CURRENT_TIMESTAMP to the DATE() function will convert the current timestamp to the current date. The CURRENT_TIMESTAMP function returns today’s DateTime(timestamp) With timezone. For instance, the following code snippet demonstrates how to convert the current timestamp to date using the DATE() function: SELECT CURRENT_TIMESTAMP, DATE(CURRENT_TIMESTAMP) The built-in DATE() function is one of the most convenient ways of converting a timestamp to a date. Method 1: Using Postgres DATE() Function - Method 2: Using Postgres CAST Operator - Method 3: Using Postgres TO_CHAR() Function - Method 4: Using Scope Resolution “::” Operator - Method 5: Using Postgres EXTRACT() Function - Method 6: Using Postgres DATE_PART() Function This blog illustrates the following methods to convert a timestamp to date in Postgres: Postgres offers various ways to convert a TIMESTAMP to a DATE, such as TO_CHAR() function, CAST operator, EXTRACT function, etc. In database management systems as well as in programming paradigms converting a timestamp to date is a common task.Ĭonverting a timestamp to a date assists us in data manipulation and analysis. These functions allow us to perform different tasks on the DateTime values efficiently. PostgreSQL is a free and open-source advanced database management system that offers numerous built-in date, and time functions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |