I am using Postgres 9.0 version. I want to add some months to a date to calculate the new date and update in a table. Here the number of months to be added will be in integer variable. My code is as follows:
declare numberofmonths smallint = 5; update salereg1 set expdate = current_date + interval cast(numberofmonths as text) month;
The above code shows syntax error at cast. I don't know how to specify the numberofmonths
variable as text.. can anyone help me. what is the mistake I did..
Instead, you should use to_char to format the date when you query it, or format it in the client application. Like: SELECT to_char("date", 'DD/MM/YYYY') FROM mytable; e.g.
PostgreSQL assumes your local time zone for any type containing only date or time. All timezone-aware dates and times are stored internally in UTC . They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.
The TO_DATE(birthday_month, 'Month') function converts a full month name to a date in the ' 0001-MM-01 ' format. For example, you get ' 0001-12-01 ' for December. You can now use the EXTRACT(MONTH FROM date) function to extract the month from this date value.
Try something like:
update salereg1 set expdate = current_date + interval '1 month' * numberofmonths;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With