Groups | Search | Server Info | Login | Register
| From | "J.O. Aho" <user@example.net> |
|---|---|
| Newsgroups | alt.php.sql |
| Subject | Re: Replace a field with a calculated value |
| Date | 2018-02-27 18:32 +0100 |
| Message-ID | <fflj1qF3113U1@mid.individual.net> (permalink) |
| References | <ftqa9d5dd424767s2rcs5ojfheonp7resr@4ax.com> |
On 02/27/18 15:57, Jim H wrote: > > I inherited a table that has a column named "date_entered" that > contains dates formatted like 1/30/2018 that I'd like to replace with > the same date formatted as 2018-01-30. What's the data type for the column in question? if you don't know, you can run the query: show columns in <table name>; > How can I replace the whole content of the field (in just one record > for now, but eventually every record) with a value calculated from the > field being replaced? > > I wish I could do something like the following, but am hitting a dead > end. > > REPLACE(date_entered, *, str_to_dat(date_entered, '%d/%m/%Y') WHERE > record_no = '123'; for an update that takes them all: update <table name> set date_entered = str_to_date(date_entered, '%d/%m/%Y') > Does replace() even allow a field name as a parameter (meaning > whatever is in that field)... or a wild card meaning the whole string > whatever it is, or does it only allow specific string values? The function replace() takes three arguments, the first is the string you want to modify, the second is the sub-string you want to change and the third one is with what, no regex. sure you could use (at least in theory): date_entered = replace(date_entered, date_entered, str_to_date(date_entered, '%d/%m/%Y') but that would be a lot slower than the update row I suggested earlier. Don't forget to change the code which inputs the date to use the format you want. I do recommend to use a datetime data type for storing time, as you can then get the format you want and change to include the time at a later point without issues on a later date. -- //Aho
Back to alt.php.sql | Previous | Next — Previous in thread | Next in thread | Find similar
Replace a field with a calculated value Jim H <invalid@invalid.invalid> - 2018-02-27 14:57 +0000
Re: Replace a field with a calculated value "J.O. Aho" <user@example.net> - 2018-02-27 18:32 +0100
Re: Replace a field with a calculated value Jim H <invalid@invalid.invalid> - 2018-03-01 17:07 +0000
csiph-web