I have a table with a row that looks like this:
(2009123148498429, '...', '...')
The first part, id, is a timestamp followed by a random number. (needed to work with other parts in the system) The data already exists in the table.
I want to create a column, timestamp, and extract just the date (20091231) and update all the rows with the timestamp.
- How can I do this for all the rows with SQL? (i.e. update them all with some sort of a function?)
- What kind of default value should I assign the column to make sure that future inserts correctly extract the date?
UPDATE - Please read the comments by bobince in the first answered question by Jonathan Sampson on how we got to the answer. This is the final query that worked:
UPDATE table SET rdate=substring(convert(rid,char(20)),1,8);
The problem was that I was using substring
as substring( str, 0, 8 )
whereas it should be substring( str, 1, 8 )
. I guess we're all used to 0 as the beginning position! More info here on substring
Related to: multiple updates in mysql