📜  postgres datetime now - SQL (1)

📅  最后修改于: 2023-12-03 14:45:33.788000             🧑  作者: Mango

Postgres Datetime Now - SQL

Postgres is a powerful relational database management system allowing developers to create and manage SQL databases with ease. One of the key features of Postgres is the ability to work with date and time values, allowing developers to store, retrieve, and manipulate this data within their databases.

Datetime Now

The now() function in Postgres returns the current date and time in the format "YYYY-MM-DD HH:MI:SS." This function can be used in a variety of ways to work with and manipulate date and time values within SQL queries.

Examples

Retrieving the Current Date and Time

To retrieve the current date and time using the now() function, simply use it in a SELECT statement:

SELECT now();

This will return the current date and time in the format "YYYY-MM-DD HH:MI:SS."

Adding Time to a Date

The now() function can also be used to add time to a date. For example, to add 2 hours to the current date and time, use the following SQL query:

SELECT now() + interval '2 hours';

This will return the current date and time, with 2 hours added.

Extracting Parts of a Date

The now() function can also be used to extract parts of a date, such as the year, month, or day. For example, to extract the year from the current date and time, use the following SQL query:

SELECT extract(year from now());

This will return the current year as an integer value.

Conclusion

In conclusion, the now() function in Postgres provides developers with powerful functionality for working with date and time values within their SQL queries. By using this function, developers can retrieve, manipulate, and extract various parts of dates and times within their SQL databases with ease.