Updating column values as per our format - mysql

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.

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.

Separate string into columns

Data :
Tree Depth
URL1||URL2 2
URL2||URL3 2
URL3||URL4||URL5 3
URL1||URL2||URL3 3
In the above data the Tree column consists of string separated by "||". I need to convert the above data such that I have 3 columns (since the max depth is 3 in this example) , the result should look like:
COL1 COL2 COL3 DEPTH
URL1 URL2 2
URL2 URL3 2
URL3 URL4 URL5 3
URL1 URL2 URL3 3
In the above example the max depth is 3 however in real world it could be N number.
Good day,
In first glance it is look like we need to use user defined SPLIT function but since number of values that you have in each string is not more then 4, there is a much simpler and probably much better solution. We just need to use the built-in PARSENAME function.
I did not test the code but the solution should be something like this:
SELECT PARSENAME(REPLACE(Tree,'||','.'), 1) as col1, PARSENAME(REPLACE(Tree,'||','.'), 2) as col2, PARSENAME(REPLACE(Tree,'||','.'), 3) as col3, Depth
from TableName
I replace the || with dot, since PARSENAME parse names that split by dot. this is the trick :-)
I actually mentioned example like this in my lecture at the sqlsaturday #360. You can see the presentation. The lecture was about WHY to use SQLCLR, and not less important WHEN to use it over transact-SQL. but I also talked about when NOT to use it, and this was one of the examples there.
In any case! if you are going to use SPLIT function then you should use SQLCLR and not T-SQL, as you can see here.
Try this, you just need to enter your Input Table, Output Table, Delimeter and Column to split. It can handle depth of more than 3, unlike PARSENAME function.
It is tested with 100,000 records and 30 split columns. It takes 10 sec to create the desired output.
Declare #Delimiter nvarchar(10) = '||'
Declare #InputTable nvarchar(2000) = '<<input table name>>'
Declare #OutputTable nvarchar(2000) = '<<output table name>>'
Declare #ColumnToSplit nvarchar(2000) = '<<column to split>>'
Declare #lsql nvarchar(max)
Declare #treeDepth int
If Object_id('dbo.treeDepth') is not null
Drop table dbo.treeDepth
CREATE TABLE dbo.treeDepth (depth INT)
declare #ltext nvarchar(max)= 'Select max(1+(len('+#ColumnToSplit+')- len(Replace('+#ColumnToSplit+','''+#Delimiter+''','''')))/(len('''+#Delimiter+'''))) from '+#InputTable
insert dbo.treeDepth EXEC(#ltext)
Select #lsql = isnull(#lsql+',','') +
'xmlname.value(''/Node[1]/Node['+cast(number+1 as nvarchar)+']'',''varchar(1000)'') AS Col_'+cast(number+1 as nvarchar)+''
from master..spt_values where type = 'P' and number < (Select * from dbo.treeDepth)
set #lsql = '
WITH ForXML
AS
(
SELECT *,
CONVERT(XML,''<Node><Node>''
+ REPLACE('+#ColumnToSplit+','''+#Delimiter+''', ''</Node><Node>'') + ''</Node></Node>'') AS xmlname
FROM '+#InputTable+'
)
Select *, '+#lsql+' Into '+#OutputTable+' From ForXML
Alter table '+#OutputTable+'
Drop column xmlname
'
EXEC(#lsql)

SQL error with variable assignment, concat, and right

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)
);

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';

MySQL Odd-Joining Issue

I had the following code:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 'not_duplicate'
and DB.T2.DUP = 'not_duplicate'
and DB.T3.DUP = 'not_duplicate'
;
The output returned 0 rows, however. So, I changed the values of the "DUP" column in each table from duplicate/not_duplicate instead to 0/1. I tried this code and it worked:
select DB.T1.ID,
DB.T1.B,
DB.T1.C,
DB.T2.ID,
DB.T2.B,
DB.T2.R,
DB.T3.ID,
DB.T3.Q
DB.T1.DUP,
DB.T2.DUP,
DB.T3.DUP
from DB.T1, DB.T2, DB.T3
where DB.T1.id = DB.T2.ID
and DB.T1.id = DB.T3.ID
and DB.T2.id = DB.T3.id
and DB.T1.DUP = 1
and DB.T2.DUP = 1
and DB.T3.DUP = 1
;
The second code works perfectly, the first one returned 0 rows. Does anyone know why was this happening? The values "not_duplicate" and "duplicate" were the exact same strings as the csv's that I imported into the database from. I can't explain why this would be the case and I'm really pretty curious.
Thanks very much!
because the DUP column they dont have this not_duplicate in fields. they have 1
then it doesnt match your query .
the query returns values which are stored in the column DUP .