Folks,
I have a column in MSSQl table as below:
| email |
-----------
suzuki#amc.com
yamaha#abc.co
harley#cbc.com
....
I want to write a query that will replace everything before the '#' in all the fields with a single work "cars". so the column should look:
| email |
-----------
cars#amc.com
cars#abc.co
cars#cbc.com
....
Any advice on how I can form this query ?
Many examples of solving this were already discussed here:
How do I replace a substring of a string before a specific character?
For example:
UPDATE YourTable set email = 'cars' + SUBSTRING(email, CHARINDEX('#',email), LEN(email))
Query:
SQLFIDDLEExample
UPDATE Table1
SET email = 'cars' +
SUBSTRING(email, CHARINDEX('#',email), LEN(email)-CHARINDEX('#',email)+1)
you try this instead ,
first select only the before # part from mail id
using this substring_index(email,'#',1)
then replace with cars
replace(email,substring_index(email,'#',1),'cars')
select replace(email,substring_index(email,'#',1),'cars') from table;
Related
In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+"
I simply need to get the column where I have multiples data and not only one.
I tried with
mycol NOT LIKE '%+'
But it didn't work...
Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'
If I had in my columns
'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'
I want to select only
'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+',
NOT 'dvgff+' !
if you want to search '..xxx+..' then you should be use xxx+%
if you want to search '..+xxx..' then you should be use %+xxx
if you want to search '..++..' then you should be use %++%
if you want to search '..+..+..' then you should be use %+%+%
It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...
so you can try like '%+%+%' to exclude just one '+'
CREATE TABLE TestTable
(`text` varchar(90))
;
INSERT INTO TestTable
(`text`)
VALUES
('jdsjpgsg+jdsjpgsg+'),
('dvgff+'),
('ffef+eefds+ghghgh+')
;
select * from TestTable
where text like '%+%+%'
| text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |
SQL Fiddle Demo Link
The % is the wildcard character. You should be using the % after data. Try like:
SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'
The above query will filter out all the records that have any characters after data+.
I have a json object in my database table field like following :
[{"user_id":"xyz","viewed":"false","answered":"false","denied":"false"}].
the other fields are id, q_id and stuff.
I want to search for the user xyz in the table!how can i do that using mysql?
JSON Parsing in MySQL Using Common_schema
Try following query using common schema
select * from tablename where common_schema.extract_json_value(tablename.columnName,'/user_id') = 'xyz';
Reference: http://mechanics.flite.com/blog/2013/04/08/json-parsing-in-mysql-using-common-schema/
If the object pattern is same across then you can use the substring_index function to parse the data, below is the example of finding the user_id from this pattern
mysql> select replace(substring_index(substring_index('[{"user_id":"xyz","viewed":"false","answered":"false","denied":"false"}]','"user_id":',-1),',',1),'"','') as user_id;
+---------+
| user_id |
+---------+
| xyz |
+---------+
Now if you want to select all the rows having user_id = xyz you can use the above as
select * from table_name
where replace(substring_index(substring_index('[{"user_id":"xyz","viewed":"false","answered":"false","denied":"false"}]','"user_id":',-1),',',1),'"','') = 'xyz';
thank you for your answers. But i just used a like query and it worked
SELECT viewers from tablename where viewers like '%\"user_id\":\"xyz\"%' && qid= 1;
In mysql there is a functionality for dba called common schema. It has json functionality.
Or
use find_in_set() function to find coma seperated values in json.
SELECT *
FROM customers
WHERE Firstname LIKE 'George'
The problem is that i have more than 1 rows in the table with tha name Geoge and the result of the query shows only one row
You will want to include the wildcard % character to include the rows the have George present in the name:
SELECT *
FROM customers
WHERE Firstname LIKE '%George%';
If George will always appear at the beginning, then you can include the wildcard on the end:
SELECT *
FROM customers
WHERE Firstname LIKE 'George%';
you need to add a wildcard character % to match any value that contains george
SELECT *
FROM customers
WHERE Firstname LIKE '%George%'
MySQL LIKE Operator
the statement
WHERE Firstname LIKE 'George'
is equivalent with
WHERE Firstname = 'George'
that is why you are only getting one record which firstname is george.
UPDATE 1
SQLFiddle Demo
try
LOWER(Firstname) LIKE '%george%'
handles partial values and avoids case sensietivity issues.
I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null
I am trying to find records that has the following scenario.
ID | name | email
1 Robert robert#gmail.com
2 William bill#gmail.com
3 Michael michael#gmail.com
4 Micahel mike#gmail.com
Based on the above table, I want to find the records where the "name" is contained in the "email field", here record 1 and 3 should be the output and not 2 and 4. Is there any way I can do this comparison?
I tried reading about regex but couldn't find anything. If it's comparison of same value, it will be straightforward, but I am not having any clue for this one. I thought of LIKE but looks like this cannot have field names.
The exact syntax will depend on how you want to define the relationship.
Are you looking for the name anywhere in the email address? (This will be slow)
select id,name,email
from your_table
where email like concat('%',name,'%')
Just at the beginning of the email address?
select id,name,email
from your_table
where email like concat(name,'%')
Just before the # sign?
select id,name,email
from your_table
where email like concat(name,'#%')
You can use LIKE, you just have to use it in combination with CONCAT.
SELECT
ID,
name,
email
FROM
yourTable
WHERE
email LIKE CONCAT(name, '%');
The CONCAT will return a string which can be used to match against email via LIKE.
This should work
SELECT * FROM table WHERE email LIKE (CONCAT('%',name,'%'))
select * from your_table where lower(substring_index(email,'#',1))=lower(name)