How to replace a part of the string using mysql - mysql

I have 100 email address in my mysql table and its look like abc#xy.com, fdgf#xy.com, ghfe#xy.com, erekke#xyz.com,tty#xyz.com, ere#xy.com, ect.... I need to change all #xy.com to #xyz.com using mysql query.
Please help me for query
Thanks in advance!

You can try this:
update myTable
set email = replace(email, '#xy.com', '#xyz.com')
where email like '%#xy.com'

Related

MySql, Copy one column into another and add some text

I have a column which has an Article-ID and one empty with the picture filename. Example: The article with the ID 34.67 should get the filename 34.67.jpg and so on. There are about 20'000 articles.
What would be the best way to do this?
Depending on your table structure or SQL dialect - it will be something like this:
UPDATE Tablename SET filename=concat(articleid, '.jpg')
OK, Thank you very much. I ended up using following code. This worked like a charm.
UPDATE oxarticles SET OXPIC1=LOWER(concat(OXARTNUM, '.jpg')) WHERE OXPIC1 = ' '

Why I am not getting result of this query ,in rails?

I am trying to get rows from mysql in rails by following query.I am trying first it on console.But this is not working,please help me.
name="vikash"
List=User.find_by_sql["SELECT * from users where name like ?",%#{name}%]
A small mistake in your query.
Space after find_by_sql and name interpolation should be done with double quote.
name = "vikash"
list = User.find_by_sql ["SELECT * from users where name like ?", "%#{name}%"]
Check below links for details
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_like
http://apidock.com/rails/ActiveRecord/Querying/find_by_sql
Hope this will help you...
Do not put variable directly into the conditions string will pass the variable to the database as-is. This means that it will be an unescaped variable directly from a user who may have malicious intent.
You can check in console by name = "vikash'" and query with the query shown by #sanju
User.find_by_sql("SELECT * from users where name like '%#{name}%'")
And see the difference how malicious characters are escaped by querying with
list = User.find_by_sql ["SELECT * from users where name like ?", "%#{name}%"]
For further information visit:
http://guides.rubyonrails.org/active_record_querying.html
https://railsguide.wordpress.com/2016/03/02/sanitizing-user-input-while-quering/
Try updating your find_by_sql to the following:
User.find_by_sql(["SELECT * from users where name like ?", "%#{name}%"])
use this code:
list= User.find_by_sql("SELECT * from users where name like '%#{name}%'")
Try this query
User.find_by_sql("SELECT * from users where name like '%#{name}%'")

How to change photopath in mysql?

I have total 5000 data in mysql how is it possible to change this photopath ?
my current data:
(photos/date/datefullname.jpg)
photos/20151117/20151117samplename.jpg
to:
(photos/applicants/date/datefullname.jpg)
photos/applicants/20151117/20151117samplename.jpg
adding applicants in photopath.
Solved using this:
UPDATE sample
SET Value = REPLACE(Value, 'photos/', 'applicants/')
you can use something like that
UPDATE table SET fieldname=REPLACE(fieldname,'photos/date','photos/applicants/date')
but you should not store the url in the database as you can change the path without edit in the database
you can see this link for more explanation
https://www.quora.com/What-are-the-ways-to-insert-an-image-path-into-the-MySQL-database-and-save-it-in-the-folder-and-display-the-same-image-in-a-web-page-via-PHP/answer/Gajus-Kuizinas

ms access like ip addresses starts with sth

In microsft access I got a table with fields username and ip
I want to select all rows that their ip addresses starts with 37.255.
I tried this but it didnt work:
select sheet1.username , sheet1.ip from sheet1 where sheet1.ip like '37.255.*'
Thanx in advance
By "I tried this but it didnt work:" you mean, you get no records back?
The SQL syntax looks good to me. Did you define [ip] as a text field? The LIKE clause would not work, if [ip] was a number.

add the same thing to a table column by sql command

there is a table named test. in it. there are some columns as this:
001.jpg
...
999.jpg
now i want to use a sql command to add a url before them. as this http://www.example.com/xxx/001.jpg.....is there a way to get this? thank you.
There is two way to accomplish this task. It depends up to you that what you want?
If you want to add a url in the database permanently then you have to use update query with no any where condition, Although if you want to only show your field with this added url you have to use select query.
Please find below the examples for both:
Suppose that your table column name is imageName then UPDATE query will be
UPDATE test SET imageName = CONCAT("http://www.example.com/xxx/", imageName);
And the SELECT query will be
SELECT CONCAT("http://www.example.com/xxx/", imageName) FROM test;
Supposing that your field is called url, a simple UPDATE query will do:
UPDATE test SET url = CONCAT("http://", url);