Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "J.O. Aho" Newsgroups: alt.php.sql Subject: Re: Replace a field with a calculated value Date: Tue, 27 Feb 2018 18:32:42 +0100 Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: individual.net k8Xo5+sPl01Szl/nppor8gf+3vnqE5GGEmabpgYZ0CH7IggUlV Cancel-Lock: sha1:hh3KFsjF/0iKbTAXkAWY5pb+ZuA= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 In-Reply-To: Content-Language: en-GB Xref: csiph.com alt.php.sql:19 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 ; > 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
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