Date Truncate

Date Truncate

Date_Truncate

  1. It will truncate and reset it to the start

Examples Of Using Date_Trunc , truncating day

  1. In PostgreSQL, you can use the DATE_TRUNC function to truncate a timestamp to the beginning of the day (midnight). If you want to reset the time component to midnight and keep the date unchanged, you can use the following:

     SELECT DATE_TRUNC('day', your_timestamp_column) AS truncated_date FROM your_table;
    
    1. In this example, your_timestamp_column is the column containing the timestamp you want to truncate. The DATE_TRUNC('day', your_timestamp_column) expression truncates the timestamp to the beginning of the day.

    2. For instance, if your_timestamp_column contains a timestamp like '2023-01-15 14:30:00', the result would be '2023-01-15 00:00:00'.

Examples Of Using Date_Trunc, truncating month

  1. If you want to "reset" the timestamp to the beginning of the month, essentially removing the day and time components and setting the day to the first day of the month, you can use a function like DATE_TRUNC in PostgreSQL or the equivalent function in other database systems.

    Here's an example using PostgreSQL:

     SELECT DATE_TRUNC('month', your_timestamp_column) AS reset_month FROM your_table;
    
    1. In this example, DATE_TRUNC('month', your_timestamp_column) sets the timestamp to the beginning of the month, with the day set to the first day (e.g., '2023-01-01 00:00:00' if the original timestamp was in January).

    2. If you are using a different database system, the function or syntax might vary. For example, in Oracle, you could use TRUNC:

    SELECT TRUNC(your_timestamp_column, 'MONTH') AS reset_month FROM your_table;
  1. Always refer to the documentation of your specific database system for the accurate syntax and functions available for truncating or resetting timestamps to the beginning of the month.