Strings with space in between - sql

I need a query to get string with space in between.
i.e it should not return strings like
' abc', 'abc ' and ' abc '
and it should return strings like
'ab c' ,' ab c', 'ab c ' and ' ab c '
i tried with below query.
select user_fname,user_lname
from user where user_fname like '% %';
but it is returning all the rows.

% matches zero or more characters. I'd suggest adding some _s in:
select user_fname,user_lname
from user where user_fname like '%_ _%';
If that's still matching too much, perhaps:
select user_fname,user_lname
from user where user_fname like '%[^ ] [^ ]%';
Which will match zero or more characters, then something that definitely isn't a space, a space, something that definitely isn't a space and then zero or more characters.

Try this:
SELECT user_fname,user_lname
FROM user
WHERE user_fname LIKE '% %' AND user_fname NOT LIKE ' %' AND user_fname NOT LIKE '% '

you could try this also
select user_fname,user_lname
from user lTrim(rTrim(user_fname)) LIKE '% %'
this will remove the white space left and right of user_fname and then it will try to find the white space between the character.

Related

Extracting Names That Has Specific Word

I would like to extract rows that has 'venture' in the Name Column as shown below.
The following SQL code is used to get that result
CASE
WHEN summary.cust_analysis.Name LIKE '%VENTURE%'
However, how can I extract only the first row which has 'Venture' as a word instead of having it as a part of a word like Bonaventure?
If I remove the '%' from the SQL code non of the rows will get extracted.
Appreciate all your help. Thank you :)
CASE WHEN summary.cust_analysis.Name LIKE '% VENTURE %'
or
CASE WHEN summary.cust_analysis.Name LIKE '% VENTURE %'
OR summary.cust_analysis.Name LIKE '% VENTURE'
OR summary.cust_analysis.Name LIKE 'VENTURE %'
OR summary.cust_analysis.Name = 'VENTURE'
with due concern for upper/lower case presumably too
If you are using SQL Server you can use Regular Expressions.
So you can match entire word followed or preceded by another symbols like dot or comma:
CASE WHEN Name LIKE '%[^A-Z]Venture[^A-Z]%'
OR Name LIKE 'Venture[^A-Z]%'
OR Name LIKE '%[^A-Z]Venture'
OR Name = 'Venture'
This will match ,Venture, Venture., Venture:
More info here
The simplest method is to prepend and postpend the string with spaces:
where concat(' ', summary.cust_analysis.Name, ' ') like '% VENTURE %'
You can use the same logic in a CASE expression:
select (case when concat(' ', summary.cust_analysis.Name, ' ') like '% VENTURE %'
then 'VENTURE'
end)
Note: This uses the CONCAT() function for string concatenation. The SQL Standard operator is || and some databases have other methods.

How to return 0 with where condition user when user search only space character?

Hello I'm using Oracle 11g and i have a data that looks like this
no|name|flag
------------
1|kumar|1
2|rajesh singh|1
3|adi sneedar|1
4|danielle castro|1
5|cef danish|1
if i did
select count(*) from tablename where name like '% %'
it will return 2 records.
if i did "multiple spaces", like 2 or more spaces
select count(*) from tablename where name like '% %'
it returns 0(this means good)
it will return 5 records.
What i want is if the user only input '% %' it will also return 0. But i also wanted that
select count(*) from tablename where name like '%adi sneedar%'
it will return 1
How should i do that in the where condition?
Something like this might suffice, assuming that the '%' are being passed as part of the binding input
select *
from mytable
where name like :bindvar
and replace(replace(:bindvar,'%'),' ') is not null
which basically says they need to enter something that is not solely spaces and percentage signs.

Removing blank spaces from the rows

I have a column as name which is having a results like.
name
ABC
XYZ
ader
fer
I want to remove the blank space before ader and it should print in the output like
ader.
How to achieve that?
Depending on your database you can use trim(), ltrim()/rtrim(), or replace():
select replace(name, ' ', '')
select trim(name, ' ')
select ltrim(rtrim(name))
You can use the LTRIM and RTRIM functions to remove trailing and leading spaces.
SELECT
RTRIM(LTRIM(name)) AS name
FROM yourTable

Oracle update multiple spaces in a column with a single space

I'm trying to update a column that could possibly have a single space or multiple spaces into just one single space using a plain sql statement not pl sql
I could do it through update table set column_name='' where column_name like '% %'
However, there could be some data such as abc def in that column. I do not want to disturb the pattern of that data meaning if want to do it only when the column is filled with white space and not touch columns that have any data.
I would recommend using a regular expression to do this, both to do the replacement and to do the matching:
UPDATE mytable
SET mycolumn = REGEXP_REPLACE(mycolumn, '\s{2,}', ' ')
WHERE REGEXP_LIKE(mycolumn, '\s{2,}')
This will replace two or more consecutive whitespace characters (spaces, tabs, etc.) with a single space. If you just want to replace spaces and not tabs, carriage returns, or newlines, use the following:
UPDATE mytable
SET mycolumn = REGEXP_REPLACE(mycolumn, ' {2,}', ' ')
WHERE REGEXP_LIKE(mycolumn, ' {2,}')
The reason for using {2,} is so that we don't bother replacing spaces where it need not be done.
With regular expression:
update table set column=regexp_replace(column, ' +', ' ')
Try:
update mytable
set col = ' '
where replace (col, ' ', null) is null;
I have used regexp_like to solve this
update table set column= ' '
where column in (select column from table where regexp_like('column','^\s+$')
Thanks
Try this:
update product set name = replace(name, ' ', ' ') where name like '% %';
where the second parameter of replace expression and like ('% %') contains two blank spaces.
In my case, the result was:
TENIS NIKE AIR => TENIS NIKE AIR
TENIS NIKE MAN => TENIS NIKE MAN

SQL SELECT field contains(words) does not work

I have this query:
SELECT *
FROM
(SELECT ' ' + REPLACE(Title,' ',' ') + ' ' AS Title
FROM MyTable) t
WHERE Title LIKE '% Samsung %'
AND Title LIKE '% Galaxy %'
AND Title LIKE '% Axiom %'
AND REPLACE(REPLACE(REPLACE(Title,' Samsung ',''),' Galaxy ',''), ' Axiom ','') = ''
This query should search in MyTable field Title and dispay all rows which contain the words specified in LIKE.
I don't get any error, but the Field Title contains a row with the following string 'Samsung Galaxy Axiom R830' and my query does not return it (and it should).
This was my original question, it worked for some records, but not for all SQL SELECT LIKE containing only specific words
Could it be because you are looking for "Samsung" with a space before & after and the string does not have a space before "Samsung" ?
You have spaces in the like. Perhaps you want something like:
WHERE (Title LIKE '% Samsung %' or title like '%Samsung' or title like 'Samsung%')
AND (Title LIKE '% Galaxy %' or title like '%Galazy' or title like 'Galaxy%')
AND (Title LIKE '% Axiom %' or title like '%Axiom' or title like 'Axiom%')
AND replace(REPLACE(REPLACE(REPLACE(Title,'Samsung',''),'Galaxy',''), 'Axiom',''), ' ') = ''
Actually, as I think about it, I think the final replace is sufficient:
where replace(REPLACE(REPLACE(REPLACE(Title,'Samsung',''),'Galaxy',''), 'Axiom',''), ' ') = ''
If the title is "Samsung Galaxy Axiom R830", then the following condition will not be true.
REPLACE(REPLACE(REPLACE(Title,' Samsung ',''),' Galaxy ',''),' Axiom ','') = ''
The replaces as written will output
SamsungAxiom R830
This will not match a blank string.
If you removed the spaces from your replaces, you'll be left with R830 (and possibly some whitespace). As Hellion says in his comment, this is a query that requires the words 'Samsung', 'Galaxy' and 'Axiom' to be the only words in your title.