Remove matched portion of a string if it matches a pattern - mysql

I have a field "model" in my table Vehicles (which has just under 59,000 entries) which may have a value of something like:
Roadline (09-14)
I want to remove the (XX-XX) if it exists and fill in the "treated" value to a field modelname.
Any help will be greatly appreciated!

A very simple solution is to use SUBSTRING_INDEX to isolate the portion before (. You can use REGEXP to make sure the pattern matches but, unfortunately, you cannot use it to capture matches.
SELECT
model,
CASE
WHEN model REGEXP '\\([0-9]+-[0-9]+\\)$' THEN SUBSTRING_INDEX(model, '(', 1)
ELSE model
END AS modelname
FROM vehicles
Once you have made sure the data looks OK, you can update the other column like this:
UPDATE vehicles
SET modelname = CASE
WHEN model REGEXP '\\([0-9]+-[0-9]+\\)$' THEN SUBSTRING_INDEX(model, '(', 1)
ELSE model
END

Related

SQL select value and then use in sub query to replace string

In my sql table I have a number of values as follows;
<![CDATA[9435547092]]>
<![CDATA[Company Name]]>
Most of the rows have a CDATA wrap, I wanted to remove this from all the files so I was thinking a subquery would be good something like;
SELECT value FROM attributes WHERE value LIKE "%<![CDATA[%";
Would give me each value and then I was thinking to do
SELECT REPLACE(SELECT value FROM attributes WHERE value LIKE "%<![CDATA[%";, "<![CDATA[", '') FROM attributes
But this isn’t valid, anyone know how this is possible?
just use replace in select for obatin the right string
SELECT replace(value , "<![CDATA[", '')
FROM attributes
WHERE value LIKE "%<![CDATA[%";
if you need store in databae then you need updated
UPDATE attributes
set value = replace(value , "<![CDATA[", '')
WHERE value LIKE "%<![CDATA[%";
but seems you are looking for the left string after LIKE "%
select right(value, length(value) - locate(value,']]')+2)
from attributes
WHERE value LIKE "%<![CDATA[%";
UPDATE attributes
set value = right(value, length(value) - locate(value,']]')+2)
WHERE value LIKE "%<![CDATA[%";
Does this do what you want?
select replace(replace(value, '<![CDATA[', ''), ']]>', '')
Note that if your actual value has "wraps", then this might affect the value. I think a more generic method would be:
select (case when value like '<![CDATA[%]]>'
then substr(value, 9, length(value) - 3)
else value
end)
Please try this,
SELECT select SUBSTRING(value,10,LEN(value)-12)
FROM attributes
WHERE value like '<![CDATA[%'
If needed where clause could be removed and as suggested above could be used with CASE statement.

Cannot check if a field contains also number

I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps

Two different queries on stimulsoft if variables are filled or not

I'm using the Stimulsoft Design to make reports and I'm using two variables to filter.
So I want to make the datasource to build one SQL statement if filter one is filled and the other is empty and another SQL if filter two is filled and the other is empty, and maybe an else statement...
So it would be something like this:
If filter one is filled and filter two is empty, then make SELECT 1, which is:
SELECT * FROM tableExample WHERE column1 LIKE '%{filter1}%'
If filter two is filled and filter one is empty, then make SELECT 2, which is:
SELECT * FROM tableExample WHERE id = '%{filter2}%'
and ID is a primary key.
Well, can I do that?
Maybe I could check it only using SQL, but I can't figure out how to do this, could someone help me please?
Thank you!
If you are looking for a single query which contains your logic you could try the following:
SELECT *
FROM tableExample
WHERE
(COALESCE(filter2, '') = '' AND COALESCE(filter1, '') <> '' AND
filter1 LIKE '%{filter1}%') OR
(COALESCE(filter1, '') = '' AND COALESCE(filter2, '') <> '' AND
id = '%{filter2}%')
Note, the somewhat ugly COALESCE calls are there because I don't know what you mean by empty. Does this mean NULL, empty string, or both?

I want to insert a value multiple time in a same field without disturbing the previous data

eg : field name = User_id
Value=abc later i want to insert xyz without disturbing abc Value= abc,xyz i want to insert efg without disturbing abc then Value= abc,xyz,efg and so on
i want to seperating each value by using ","(comma). can any one help me out
In MySQL you could often refer to the value of a column just by using the column name. And to concatenate strings with a separator there's a nifty function called concat_ws (concat with separator).
In your case the code would look something like
UPDATE YourTable
SET Value = CONCAT_WS(',', Value, 'cde')
WHERE User_id = 123;
Good Luck!
MySQL CONCAT_WS() function is used to join two or more strings with separator. The separator specified in the first argument is added between two strings. The separator itself can be a string. If the separator is NULL the result is NULL.
Click hear for more information

Append a "+" at the beginning of every field without a "+"

I need to write a query that appends a "+" on the front of every p2_number meta_key that doesn't already begin with a "+". The name of the table is "wp_4_postmeta". I attached an image of the database so you can see what I'm talking about. http://mmw-file-sharing.s3.amazonaws.com/2015/Screen%20Shot%202015-07-30%20at%204.12.25%20PM.png
UPDATE aTable
SET someField = CONCAT('+', someField)
WHERE someField NOT LIKE '+%'
;
If someField is also indexed, the query should be fairly quick as well.
Update table set column = concat("+", column) where column not like "+%"
Should do it
You can check using substr if the first character is a +. If so, return just the field value, else prepend a +.
select
case when substr(p2_number, 1, 1) = '+' then
p2_number
else
concat('+' p2_number)
end as p2_numberplus
from
wp_4_postmeta
Or do you mean actually updating the table data? In that case, Uueerdo's answer is the one you want.