Apostrophe replacement in mysql query - mysql

I have in a column names(notes) as
Notes:
John's Table
Smith's Window
Column contains an apostrophe in it. I am trying to run 2 queries.
Select query to get the column entries that have apostrophe.
Update those with apostrophe with empty string ie.John's Table replace to John Table
select notes from table1 where notes like ' '% ';
I get a syntax error , any help will be great.

Escape the apostrophe with a backslash;
select notes from table1 where notes like '%\'%';
And to update, use REPLACE()
UPDATE table1 SET notes = REPLACE(notes, '\'', '');

Did you try:
select notes from table1 where notes like "'%";

Related

Oracle SQL - Regex to search columns with only 1 letter

I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')

remove whitespace in between Column values in mySQL

How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');

group_concat text field in mysql

Tried to group_concat a text field in mysql.But its taking only the first value.(if the values are '177,178') it taking only 177 because I am saving this as a text field.So how can I do group_concat with this fields?
My query looks as such:
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ') FROM abc WHERE abc.lookupId IN ('177,178')
Are you misplacing the quotes within your IN?
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ',') FROM abc WHERE abc.lookupId IN (177,178)
The problem isn't with GROUP_CONCAT, the problem is that your WHERE clause is only selecting one row. Since the lookupId field is an integer, it's converting the string '177,178' to an integer, which is just 177. You shouldn't have quotes around the values in the IN() list, that's making it just a single value to look for.
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ')
FROM abc
WHERE abc.lookupId IN (177, 178)
If the comma-separated string is actually coming from a column in a table you're joining with, see sql join tables where 1 column has comma.
SELECT GROUP_CONCAT(table1.displayValue SEPARATOR ' ')
FROM table1
JOIN table2 ON FIND_IN_SET(table1.lookupID, table2.lookupIDs)
WHERE ...

SQL - select all rows from table where a column contains only digits

I want to find all rows in which a column contains only digits.
The first idea was using LIKE [0-9]%, but in fact this matches any strings starting with a digit.
It seems to be a very simple task, but unfortunately I can't find solution
In Sql server use Not Like
where some_column NOT LIKE '%[^0-9]%'
Demo
declare #str varchar(50)='asdarew345'
select 1 where #str NOT LIKE '%[^0-9]%'
SQL FIDDLE DEMO
For Mysql use REGEX
SELECT *
FROM yourtable
WHERE string_column REGEXP '^[0-9]+$'

MySql find a value in a comma separated set

I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'