SQL queries to replace wildcard text? [duplicate] - mysql

This question already has answers here:
MySQL string replace
(6 answers)
Closed 3 years ago.
so I have a small database I'm fooling around with and trying to get better at. I currently have a table filled with employee names and notes (just a mock up) so the result would look something like this:
Name / Employee # / Note
where it includes the employees name, their number, and a written note about the employee. I have written some sample text in the notes like "Is a great worker, never late - Adam"
SELECT * FROM `employees` WHERE `Note` LIKE '%Adam%'
I can use that to display all the notes that were posted by Adam, but lets say I want to change the name from Adam to Brad without having to rewrite every note, is there a way to replace that specific text but keep the rest of the note?
Thanks

You can use the following query:
update employees set Note = REPLACE(Note, 'Adam', 'Brad');
Check out this link for more info:
MySql Reference Manual

Related

MySQL question about getting concret columns [duplicate]

This question already has an answer here:
MySQL LIKE query with underscore
(1 answer)
Closed 4 years ago.
I have a table in my database that contains information like this:
activity: R1A1, R1A2, R1A2_2
My problem is where I'm trying to get all the activities that do not contain the second part (_2).
I'm trying this query: SELECT activity FROM table_name WHERE activity NOT LIKE '%_2' but I'm getting only the activity R1A1. This is because R1A2 contains a number two on its name. How can I solve it? What is the correct query to do that? I want to get all the activities without _2 or something like that on its name.
If I do in an inverse method (SELECT activity FROM table_name WHERE activity NOT LIKE 'R1%' I get the correct results.
How can I get what I need? Thanks for your answers!
in MYSQL, the underscore is a wildcard, much like the % except it matches one character. You can escape it with \ to get the literal.
Here is the correct query:
SELECT activity FROM table_name WHERE activity NOT LIKE '%\_2'
The underscore in LIKE is a wildcard that matches any character. You can escape it. Here is one method:
SELECT activity
FROM table_name
WHERE activity NOT LIKE '%$_2' ESCAPE '$';

How can search for something and then change some text in table column by MYSQL [duplicate]

This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have some records in MYSQL database with domain name, Now i changed domain and i want use query for find these word example.com in record column and then change to example.net.
I think if column only include domain, it's very easy but my records include domain name and some text. Like this:
it's a sample text www.sample.com and i want change it
I have some recorde like above and i want find every recorde that have sample.com text and then change it to sample.net
You can use a simple REPLACE() for this:
UPDATE <table> SET <column> = REPLACE(<column>, 'www.sample.com', 'www.example.net');
The above will replace all occurances of www.sample.com with www.example.net in the specified column.
UPDATE mytable SET mycolumn = REPLACE(mycolumn,'www.sample.com','www.sample.net');
Replace

how to put comma in a money data type such like Decimal [duplicate]

This question already has an answer here:
mysql select int as currency or convert int to currency format?
(1 answer)
Closed 9 years ago.
I have a column by the name of Currency in which I store monetary values. I have set the data type of this column to decimal. My question is can I have comma's after 3 digits when i am displaying the money on a webpage.
For example I would like to display it as: USD980,443,22.00
Currently its displaying like this: USD98044322.00
Is it possible, or are there any other ways to do that, because I have seen it in websites lots of times.
Typically you would want to do that in your server-side language so as to take the load off the DB, for example in PHP:
USD<?php echo number_format($dollarsAndCents, 2)?>
But if you're dead set on doing it in MySQL, here's how:
SELECT Format(dollarsAndCents, 2);

How to UPDATE just part of a text entry in mysql? [duplicate]

This question already has answers here:
Replace a word in BLOB text by MySQL
(3 answers)
Closed 9 years ago.
I have a column in my database that has the absolute URLs to images.
I've just transferred the entire website to another folder so the URLs of the images have changed. So for eg. if the URL of an image in the image_URL column was like:
http://www.mysite.com/images/myimage.jpg
I need to update it like this:
http://www.mysite.com/newfolder/images/myimage.jpg
The type for the image_URL column is TEXT. But I need to update it ONLY if the URL being used is "mysite" and not "externalsite".
What's the right SQL to use? I'm very familiar with SQL UDATE commands but not where I need to update only PART of a column value.
UPDATE table SET image = REPLACE(image, "http://www.mysite.com/images/", "http://www.mysite.com/newfolder/images/")
Sure, just do a LIKE search for 'mysite' to get all the URL's. Then one by one, change the text and resave it to your dB.
lee

Extracting MySQL data within "tags" using regular expressions? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Simulating regex capture groups in mysql
Good day,
I have many rows of data stored in a MySQL table. A typical value could look something like this:
::image-gallery::
::gallery-entry::images/01.jpg::/gallery-entry::
::/image-gallery::
Is there a way - by means of a regular expression that I can a) extract the term image gallery from the first line (it could be any phrase, not just image-gallery) and then extract the center line as two separate values like this:
gallery-entry and then images/01.jpg
There could be many lines of ::gallery-entry:: values, and they could be called anything as well. A more complete example would be:
::image-gallery::
::title::MY GALLERY::/title::
::date::2011-05-20::/date::
::gallery-entry::images/01.jpg::/gallery-entry::
::/image-gallery::
In essence I want this information: The content type (image-gallery) in the above case, first line and last line. Then I need the title as a key value style pair, so title as the key and MY GALLERY as the value. Then, subsequently, I would need all the rows of fields thereafter (gallery-entry) as key value pairs too.
This is for a migration script where data from an old system will be migrated over to a new system with different syntax.
If MySQL select statements would not work, would it be easier to parse the results with a PHP script for data extraction?
Any and all help is always appreciated.
Kind regards,
Simon
Try this regex:
::image-gallery::\s+::title::(.*?)::/title::.*?::gallery-entry::(.*?)::/gallery-entry::\s+::/image-gallery::
Use single-line mode (/pattern/s) so the .*? chews up newlines.
Your key-value pairs will be:
title: $1 (matching group 1)
gallery-entry: $2 (matching group 2)
From simulating-regex-capture-groups-in-mysql there does not seem to be a way to easily capture groups with a regex in mysql. The reason is that MySQL does not natively support capture groups in a regex. If you want that functionality you can use a server side extension like lib_mysqludf_preg to add that capability to MySQL.
The easiest way is to extract the whole column with SQL and then do the text matching in another language (such as php).
In my tests kenbritton's regex didn't work, but building off of it the following regex worked on your test data:
::image-gallery::\s+::title::(.*?)::\/title::\s+(?:.*\s+)*::gallery-entry::(.*?)::\/gallery-entry::\s+::\/image-gallery::