1

I have a table 'setlists' in mysql with columns 'year' 'month' 'day' of int type that I would like to str_to_date and concat into a new column 'date' in the same table. This is what I have so far but I cant seem to get the syntax right. It wont run. I have already created the new date column and set it as date type.

UPDATE setlists
SET date = str_to_date(concat(year,'-',month,'-',day), '%Y-%m-%d') FROM setlists

Data:

I am trying to concat the year month day columns into the date column of the same table.

date  year  month  day
null  2018  01     01
null  2017  01     01

The current error output is as follows:

Error Code: 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 setlists' at line 2

4 Answers4

4

Just remove FROM setlists at the end of your update query. It does not belong there.

UPDATE setlists
SET date = str_to_date(concat(year,'-',month,'-',day), '%Y-%m-%d');

SQL update syntax is such that the target table is specified as whatever follows UPDATE.

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
1

With update queries you should not use the FROM [table], unless you use a SELECT query for input, you already define which table you want to update with UPDATE setlists.

Remove the FROM setlists from your query to make it work.

See the UPDATE query syntax documentation here

So:

UPDATE setlists
SET date = STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%Y-%m-%d');

See: SQL Fiddle

Robert
  • 4,840
  • 1
  • 24
  • 29
0

Check the usage of year,month,day functions

   UPDATE setlists
    SET date = str_to_date(concat(year(sysdate()),'-',month(sysdate()),'-',day(sysdate())), '%Y-%m-%d');

If these are columns then

UPDATE setlists
SET date = str_to_date(concat(year,'-',month,'-',day), '%Y-%m-%d');
Kedar Limaye
  • 1,031
  • 6
  • 14
0

There's a neater way:

DATE(CONCAT_WS('-', year, month, day))
Zon
  • 12,838
  • 4
  • 69
  • 82