SQL error with variable assignment, concat, and right - mysql

I'm trying to generate an ID by concatenating bits from a few cells in a MySQL table. I want t0 get rid of - and : and only have digits in the ID. I get a syntax error with the following:
update scan_data
set #scanDate1 = replace(scanDate,'-','')
set #scanTime1 = replace(scanTime,'-','')
scanID = concat(right(scanContent,2),right(#scanDate1,2),right(#scanTime1,2))
What do I need to change?

Try this
update scan_data set scanID = concat(right(scanContent, 2),
right(replace(scanDate,'-',''), 2),
right(replace(scanTime,'-',''), 2)
);

Related

mysql update json attribute and another column in one query

I need to update a json value in a column as well as update another column in the same query.
Something like this:
UPDATE fixtures
SET jsonResults = '{}',
JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
How can I accomplish this?
JSON_SET() returns a JSON document value, but an UPDATE statement needs a series of assignment expressions:
UPDATE fixtures
SET jsonResults = '{}',
jsonFixture = JSON_SET(jsonFixture, '$.time_status', '0')
WHERE intRefID = 88961323;
This replaces jsonFixture with the result of JSON_SET(), after setting a field within that document.
Compare with an UPDATE like this:
UPDATE mytable
SET i = i + 1
WHERE ...
It takes the value of i, adds 1, and then uses the result of that addition expression to replace i.

Update 500+ field records to include an increment value + attribute value

Im looking to update 500+ records in my mysql database so that the fields will be a value combination of an $incremental_value+db_user_first_name+#some_static_text. An example of the wished outcome:
1_firstname#staticstring.com, 2_george#staticstring.com, 3_johnny#staticstring.com etc.
I've been playing around with some approach as the following, but that naturally doesn't work (modified for hopefully better clarification).
UPDATE user
SET email = (($incremental_value+1)+(user.first_name))"#staticstring.com"
WHERE email = "empty#empty.com"
The correct syntax for string concatenation in MySQL is the concat() function:
UPDATE user cross join
(select #i = VALUETOSTART) var
SET email = concat(#i := #i + 1, '_', user.first_name, '#staticstring.com')
WHERE email = 'empty#empty.com';

SQL: perform same operation on all rows

I'm using MySQL and I have a column of dates that were enterered into the database incorrectly. More specifically, they were entered in as dd/mm/yy but MySQL assumed they were in the format yyyy/mm/dd. I made the following code to correct this problem but there is an error stating that the subquery in the first line returns more than one row. How can I make it perform this operation on every row? Do I need a loop?
SET #raw_date = (SELECT j.a_date FROM tbl_job j);
SET #first = SPLIT_STR(#raw_date, '-', 1);
SET #second = SPLIT_STR(#raw_date, '-', 2);
SET #third = SPLIT_STR(#raw_date, '-', 3);
SET #first = (SELECT RIGHT(#first, 2));
SET #job_date = CONCAT(#third,'-',#second,'-',#first);
UPDATE tbl_job
SET tbl_job.a_date = #job_date;
SELECT j.a_date FROM tbl_job
UPDATE tbl_job
SET tbl_job.a_date = #job_date
eliminate WHERE clause, it will update all rows...

Updating column values as per our format

There are two types of records in my Db such as MS-NW and CS in the same column of table DICIPLINE I want to wrap if its CS (ANY TWO STRING LIKE CS,TE OR THE LIKE) then wrap it to BS(CS) (OR BS(TE) ETC) or if its MS-NW (Or MS-CS, MS-TE and the like) then wrap it to MS(NW) from the column dicipline.
I updated for two strings successfully and following is the query for that kindly let me know how can i do it for values like MS-NW OR MS-CS and convert it to the format like MS(NW) from following query .
UPDATE DEG set DICIPLINE = concat("BS(",DICIPLINE,")") where CHAR_LENGTH(DICIPLINE) = 2
The below query helps you to update your data.
update deg set DISIPLINE = if(length(DISIPLINE)= 2,concat('BC(',DISIPLINE,')')
,concat('MS(',substr(DISIPLINE, 4,4),')'));
See Sqlfiddle demo.
For safety, create a temporary column of same type and perform an update like this:
UPDATE deg
SET dicipline_temp = CASE
WHEN CHAR_LENGTH(dicipline) = 2
THEN CONCAT('BS(', dicipline, ')')
WHEN CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-'
THEN CONCAT(REPLACE(dicipline, '-', '('), ')')
END
WHERE CHAR_LENGTH(dicipline) = 2 OR (CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-')
If results are acceptable, update the actual column.

How to use concat() to append to multiple columns in a single mysql update

I can't figure out the syntax for Mysql update with multiple concatinations. I want to be able to append a string to the end of the string stored in the database but do it to multiple columns all at once. I can do one column at a time just fine with this
UPDATE `table1`.`column1` SET `category1` = CONCAT(category1,'$value[0]',) WHERE `id`='$id';
But when I try to do it to multiple columns in the same table I get a syntax error.
UPDATE `table1`.`column1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';
"You have an error in your SQL syntax;"
I can't find the syntax for separating each concat.
According to MySQL docs, UPDATE does not support such syntax. You must reference the table name, without the column, before the SET:
UPDATE `table1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';