question

Gav 1 avatar image
Gav 1 asked

Update datetime by one year on sql database

Hi, I have a table in my database called "listingsdbelements" and each property has an available date which is in 'datetime' ie '1200268800'

I need to increase every one by exactly 1 year.

Within the table there is a column called "listingsdbelements_field_name" so when the value of "listingsdbelements_field_name" is "available" there is a value in the next column "listingsdbelements_field_value" with the date time value ie "1200268800".

Can this be done?

datetimetime
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kristen avatar image Kristen ♦ commented ·
What will be the desired date if you add one year to, say, 29-Feb-2008 - i.e. a leap year? Note that whatever you do (round down to 28-Feb or round up to 01-Mar) if you increment it by one year each year it won't get back to being 29-Feb in four years time - to achieve that you will need to store a Base Date and a Display Date which is N years added on (calculated from the Base Date each time)
0 Likes 0 ·
Madhivanan avatar image
Madhivanan answered

What is the date value of '1200268800'?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gav 1 avatar image
Gav 1 answered

1200268800 is 14/01/2008

I am unsure if the date format matters but I use DD/MM/YYYY

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gav 1 avatar image
Gav 1 answered

Hi, I know its unix time, I would like to add 1 year ie (60x60x24x36) in seconds to each value. So is there an sql statement I can add which does this for the values and database model I mention above?

ie.

To get 1 year in front of 1200268800 I would need to add 3110400 which then translates 14/01/2008 into 14/01/2009

Im not too bothered about leap years I just need to add in BULK to the 800 properties which have a unix datetime.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kristen avatar image
Kristen answered

Assuming this is SQL Server, and that I have understood the table / column name requirements correctly!!, you should be able to do this with

            
UPDATE U            
SET listingsdbelements_field_value = listingsdbelements_field_value + 3110400            
FROM listingsdbelements AS U            
WHERE listingsdbelements_field_name = 'available'            

Take a backup first!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gav 1 avatar image
Gav 1 answered

Hi Kristin, Thanks for posting this. I ran the SQL statement and received an error back

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM default_en_listingsdbelements AS U WHERE default_en_listingsdbelements_fie' at line 3 "

I am unsure of the exact SQL version but I know it has recently been upgraded on my server.

I added default_en_ to each statment prefix too..

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Madhivanan avatar image
Madhivanan answered
UPDATE listingsdbelements             
SET listingsdbelements_field_value = listingsdbelements_field_value + 3110400            
WHERE listingsdbelements_field_name = 'available'            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ilyas avatar image
ilyas answered
i want to one hour with min e.g. 10-jan-2012 21:20 to 10-jan-2012 22:20 what is date format or query for using in forms
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
You should have asked a separate question... What you need is the DATEADD function.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.