Removing part of string from field in MySQL view - mysql

I am moving data from Oracle to MySQL, this data is relevant for several views.
In one View, part of a string needs to be stripped from the result in one field of the view - if it exists.
So 643726493.234 would be fine, but 643726493.234-tzuv would need to turn into 643726493.234.
(How) can I do that?

If the string have always the - charachter separating the two part you can use somethings like this
select substr(your_column, 1, POSITION('#' IN your_column)-1) from your_table;
or for update
update your_table
set your_column = select substr(your_column, 1, POSITION('#' IN your_column)-1);

Related

Select a specific part of a value from a column in mariadb

i´m new in SQL and want to solve following case.
I have a table in a MariaDB named inventories. In this table are 2 columns: identifier and data.
I want to select and output a specific part in the column data, if the identifier is a specific one.
For example:
identifier
data
steam:xxxxxxxxxxxxxxx
...some data...,"Serial":"ZF3CJ8L1rrKjwP7nKTzb",...some more data...
The only part, that i want in the output is this: "Serial":"ZF3CJ8L1rrKjwP7nKTzb" but the part behind "Serial":" is a random value.
How can i solve this?
(i put a screenshot in here, table does not work for me in editorscreenshot of table in stackoverflow editor.)
You can try using the SubString function, which only returns a certain amount of character and starts a whatever character you want.
I got it!
SELECT (SUBSTRING(data, LOCATE('"Serial":"', DATA) + 0, 31)) FROM inventories where identifier = 'steam:xxxxxxxxxxxx';

MYSQL Update part of String if that piece is not in the string and do nothing if it's there

I have got a string like this
****1****2****22****111****3****34****21****33****23****213****213****222****55****152****36
Let's say I have a new number to add to this string like ****44,
I want to UPDATE the string and add that number ****44 Only if it is not already in there.
If a number is already there i.e. Suppose I want to add ****22, the UPDATE should not happen because ****22 is already in the string.....
However, if I have ****22222, the UPDATE should Proceed.
How can this possibly be done:
So far I have this great thoughts from this SO Link: MySQL string replace
However, using this code (got from the SO Link as the Accepted answer there):
UPDATE your_table
SET your_field = REPLACE(your_field, 'articles/updates/', 'articles/news/')
WHERE your_field LIKE '%articles/updates/%'
I dont get how the checking of if the piece already exists will be done cause, this seems to be a direct change from one string to another.
Any Suggestion is honored.
Assuming the column you want to add data to is of a character type this should work:
update your_table
set your_column = concat(your_column, '****22')
where your_column not like '%****22*%'
and your_column not like '%****22';
The first where condition filters out cases where****22would match a longer similar sequence like****222in the middle of the string by checking that the match is followed by a*and the second condition filters out matching rows that end with****22and thus are not followed by any*.
This will append****22at the end ofyour_column
Sample SQL Fiddle
The following statement will update all rows and tack on a '22' to the end of 'myval' if it doesn't have '22' in the value somewhere. If you're literally dealing with *** characters, you have to escape them in the regexp.
update foo set myval= if(myval regexp '22',myval,concat(myval,'22'));

How can i separate the data from selected data

I have a column in my table name as URL and it contains Multiple value like "https://www.google.com/#q=how+to+make+a+android+app"
and
http://www.bing.com/search?q=how+to+make+a+android+app&go=&qs=n&form=QBLH&pq=how+to+make+a+android+app&sc=8-15&sp=-1&sk=
I want to get data separately in output like
website = https://www.google.com
Keyword = how to make a android app.
Any Idea Plz, How can i get this in MySql.
you can use substr() function in php to cut string. In your case, you can get the characters up to the end of 'https://www.google.com'.
read more at http://ro1.php.net/substr
in case you want to do it directly in your query, you can use substr() as well. Sql syntax goes like this:
SELECT SUBSTR(column, start_position, desired_length) FROM table_name
*SELECT SUBSTR(column_name, 1, 20) FROM table_name;*

How to remove -XXXX from Zip Code field using MySQL REGEXP?

Can someone give me an example of how to remove anything after a dash "-" in a zip code field using SQL commands?
For example, change any of this:
XXXXX-X
XXXXX-
XXXXX-XX
XXXXX-XXXX
to this:
XXXXX
Thanks for the examples. I also need to remove any instances of "-", "-X', "-XXXX", etc in the databases so the zip codes just contain five digits. Can someone include an example of this?
Given that your field is named zip , then just do this:
SELECT SUBSTRING_INDEX(zip, '-', 1) as zip
that would return what you want.
To update the data on the table you can do:
update table set zip = SUBSTRING_INDEX(zip, '-', 1) where condition = foo
that would update only records matching some condition, if you want to update them all remove the where part
Instead of using a regex, you could use MySQL's SUBSTRING_INDEX() method:
SELECT SUBSTRING_INDEX(zip_code, '-', 1) FROM your_table
EDIT (to support updates)
UPDATE your_table SET zip_code = SUBSTRING_INDEX(zip_code, '-', 1);
I'd recommend creating a second column, maybe zip_code_short and running SET zip_code_short = instead of overwriting the main data - just to make sure it doesn't cause any errors first (if feasible).

How to update Mysql row that has serialized data?

I have 2000 products with row that is using serialized data and I need to update specific string
this is the row name data
a:35:{s:11:"expire_days";s:3:"30d";s:12:"trial1_price";s:0:"";s:11:"trial1_days";s:0:"";s:12:"is_recurring";s:0:"";s:10:"start_date";s:0:"";s:5:"terms";s:24:"$150 for 1 Per license";s:12:"rebill_times";s:0:"";s:15:"paypal_currency";s:0:"";s:4:"##11";N;s:3:"url";s:0:"";s:8:"add_urls";s:0:"";s:4:"##12";N;s:5:"scope";s:0:"";s:5:"order";s:4:"1010";s:11:"price_group";s:1:"7";s:13:"renewal_group";s:2:"28";s:14:"need_agreement";s:0:"";s:13:"require_other";a:1:{i:0;s:0:"";}s:16:"prevent_if_other";N;s:4:"##13";N;s:19:"autoresponder_renew";s:0:"";s:16:"dont_mail_expire";s:0:"";s:13:"joomla_access";s:2:"36";s:10:"files_path";s:108:"products/Boxes8.zip|Box 8
products/Boxes9.zip|Box 9";s:14:"download_count";s:0:"";s:18:"download_unlimited";}
and only thing I need changed is
s:24:"$150 for 1 Per license";
any help is appreciated.
You should probably SELECT the row, make your changes, then UPDATE with the new value. The answer to this question may be helpful if you need to do this database side.
How to do a regular expression replace in MySQL?
If you want to replace the value of that single field with something else, you can use the following query:
UPDATE table SET col = CONCAT(
LEFT(col, LOCATE('s:24:"', col) + 5), -- up to and including the opening quote
'Now for free', -- new replacement text
SUBSTR(col, LOCATE('"', col, LOCATE('s:24:"', col)+6)) -- closing quote and everything after that
) WHERE col LIKE '%s:24:"$150 for 1 Per license"%'
Note that there is potential for trouble: if the value of one of your fields should end in 's:24:', then that combined with the closing quote would get misinterpreded as the location you're looking at. I consider this risk unlikely, but if you want to play it safe, you might want to check for that with an elaborate regular expression that can deal with quoted strings and escaped quotes.