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

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.

Related

Remove matched portion of a string if it matches a pattern

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

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?

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.

MySQL get all characters before specific character

I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..
you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.
Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...

MySQL query to prepend character to each entry

I have a table of users which has a username column consisting of a six digit number e.g 675381, I need to prepend a zero to each of these usernames e.g. 0675381 would be the final output of the previous example, is there a query that could handle this?
UPDATE Tablename SET Username = Concat('0', Username);
what type is the column of?
if it's string type, try something like this:
UPDATE your_table SET column_name=concat('0',column_name);
You mean "prepend" ? i.e. add it on the front?
Is the column numeric? Do you always want 7 characters output?
Assuming that, something like this would work for a query:
select LPAD(CONVERT(username, CHAR), 7, '0')
If the column is characters, the CONVERT() part is unnecessary, just LPAD the username.
If you want to permanently modify the value in the table, you'll need to ensure the column is a character type and UPDATE using the above.
You might want to use CONCAT_WS('', '0', Username) because if there is a null value, then you'll end up with NULL instead of '0'. This probably isn't a problem, but something I've learnt the hard way.