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

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.

Related

update specific column of mysql table

I have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)

Mysql query that replaces a unknown number in a path

Lets say that you have the following stored in table:
{2:22}{4:5}{34:4}
I what to delete {4:5} from this string but the system dosent know what the number after the ":" is just the first one. The query looks something like this:
UPDATE tbl SET this = REPLACE(this,'{4:??}','') WHERE id = 1;
What do i need to put in ?? place to return the following result?
{2:22}{34:4}
Here's one way to do it using LEFT, SUBSTRING, LOCATE and REPLACE:
update yourtable
set yourcolumn =
replace(yourcolumn,
Left(
Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)),
Locate('}',Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)))),
'')
SQL Fiddle Demo

Concatenate Field with Plain Text in CASE used in Select?

I may end up having to do this with my PHP on the result set, but I was hoping the DB could do this for me. I'm wanting to check a field and if it matches a condition, set another field equal to another field + a string. Here's my attempts:
SELECT CASE UnityReq WHEN '1' THEN Table + ' (Unity)' ELSE Table END AS Type
If the Table were equal to Event, I'd want to get 'Event (Unity)' as Type.
I saw in a different post you have to cast your field if combining with a string, so I tried this:
SELECT CASE WHEN UnityReq = 1 THEN CAST(Table AS NVARCHAR(200)) + ' (Unity)' ELSE Table END AS Type
And tried the MySQL CONCAT function:
SELECT CASE UnityReq WHEN 1 THEN CONCAT(Table, ' (Unity)') ELSE Table END AS Type
I've also tried both WHEN's with or without the quotes around the number (as it's an INT field), and about every format I've been able to find googling for CASE, but I'm always getting SQL syntax errors. I feel like I've tried every version of this command I can come up with but I'm not having any luck. Any pointers?
It's not CASTing you're after, but CONCATenation.
Something like:
SELECT
CASE WHEN UnityReq = 1 THEN CONCAT(Table, ' (Unity)') ELSE Table END AS Type
FROM ...

MySQL Update command

i need to add a special text to all rows in my mysql table ,
how to add some text to the end of all rows' content in a table just for one field
i used this code :
UPDATE `blogs` SET `title`= `title` + 'mytext';
but didnt work for me
UPDATE blogs SET title=concat(title, 'mytext');
MySQL does not have a string concatenation operator (+). You have to use the concat() function, as Daniel Schneller pointed out in the other answer.
If you try SELECT '1' + '2'; in MySQL, it would return 3. The + operator is simply an addition operator. Your query would have updated the title field with a 0.

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.