I have a variable passed to stored procedure
Ex:
#keywords = 'val1, val3, val5'
And i'm trying to see if column named Title contain any of them in it
Ex: Title1 - 'Hello val1'
Title2 - 'Hello val3'
Title3 - 'Hello val1, val3'
Title4 - 'Hello'
SO my results should return values
Title
------
Hello val1
Hello val3
Hello val1, val3
Is this possible to use LIKE or any other function/method?
You need to split the CSV into rows (see Arrays and Lists in SQL Server 2005 and Beyond for variuos techniques how). I'll assume that you create dbo.ufnSplitRows based on this
Then JOIN using LIKE
SELECT *
FROM
MYtable M
JOIN
dbo.ufnSplitRows (#CSV) C ON M.Title LIKE '%' + C.SplitValue + '%'
By the way, it will run poorly because of the leading '%' at least
If you make some assumptions about how the query string is stored, then yes (though it's not terribly efficient):
Assumption: the string will be stored with every item separated by a comma, then a space. (This is what you posted in your question)
select * from YourTable where
(#keywords like KeyColumn + ', %') or
(#keywords like '%, ' + KeyColumn + ', %') or
(#keywords like '%, ' + KeyColumn)
Related
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.
My SQL Server database table has a column text which is a long string of text.
The search list is a string of words separated by comma. I want to grab those rows where the text column contains any one of words in the string.
DECLARE #words_to_search nvarchar(50)
SET #words_to_search = 'apple, pear, orange'
SELECT *
FROM myTbl
WHERE text ??? --how to specify text contains #words_to_search
Thanks a lot in advance.
If you're running SQL Server 2016 or later, you can use STRING_SPLIT to convert the words to search into a single column table, and then JOIN that to your table using LIKE:
DECLARE #words_to_search nvarchar(50)
SET #words_to_search = 'apple,pear,orange'
SELECT *
FROM myTbl
JOIN STRING_SPLIT(#words_to_search, ',') ON text LIKE '%' + value + '%';
Demo on SQLFiddle
Note that as the query is written it will (for example) match apple within Snapple. You can work around that by making the JOIN condition a bit more complex:
SELECT *
FROM myTbl t
JOIN STRING_SPLIT(#words_to_search, ',') v
ON t.text LIKE '%[^A-Za-z]' + value + '[^A-Za-z]%'
OR t.text LIKE value + '[^A-Za-z]%'
OR t.text LIKE '%[^A-Za-z]' + value;
Demo on SQLFiddle
First, I would use exists, unless you want to return the matching word.
Second, you can do this with a single like comparison If words are separated by spaces:
select t.*
from t
where exists (select 1
from string_split(#words_to_search, ',') s
where ' ' + t.text + ' ' like '% ' + value + ' %'
);
For more generic separators, you can use:
select t.*
from t
where exists (select 1
from string_split(#words_to_search, ',') s
where ' ' + t.text + ' ' like '%[^A-Za-z]' + value + '[^A-Za-z]%'
);
Or whatever describes your separators.
Note that your list of words is separated by a comma-space, not just a comma. However, based on your description (not the sample data), I have only used a ',' for the separator.
I've been working on splitting out some names into several columns in MS SQL server, and what i've got left is a last name and an amidst value (John OF THE Smithsons)
So heres what I want to do, I've got several examples:
of Holland
Of the Clothson,
England, from
So I've got 3 variations of this, (of, of the, from) which can be in random places in the string. What I want to do is remove these values from the LastName column and move them to the Amidst column so I'm left with,
LastName|Amidst
--------------
Holland |of
clothson|of the
England |from
what would be the best way to do this? Is it possible to move the values I want to select into a table and reference from there? I'm not sure if this is possible.
From your description, giant case expressions might be the best way:
select (case when col like '% of the%'
then ltrim(rtrim(replace(col, ' of the', '')))
when col like '% of%'
then ltrim(rtrim(replace(col, ' of', '')))
when col like '% from%'
then ltrim(rtrim(replace(col, ' from', '')))
else col
end) as new_col,
(case when col like '% of the%'
then 'of the'
when col like '% of%'
then 'of'
when col like '% from%'
then 'from'
end) as new_midst
I am trying to change the Titles of 'doctors' in a database and was just wondering was there a SQL query which I could run to change them.
The column im trying to change
What I am asking is that there is any way I can update the column to add a 'Dr' infront of the names to replace the 'Miss','Mr' etc.
I'm thinking about using a SQL Statement containing the wildcard function to update it but not sure it would change the specifics.
Thanks,
Karl
Use REPLACE option
UPDATE table_name
SET column_name = REPLACE(column_name, 'Mr.', 'Dr.')
WHERE column_name LIKE 'Mr.%'
try this
Update myTable
set name = replace(replace(name,'Miss ','Dr '),'Mr ','Dr ')
I might suggest doing:
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr');
This only replaces the first word in the string. You might want to be extra careful and add a where clause.
update t
set col = stuff(col, 1, charindex(' ', col + ' '), 'Dr')
where col like 'Miss %' or col like 'Mr %' or
col like 'Mrs %' or col like 'Ms %';
The problem with replace() is that it replaces all occurrences in the string. Although the honorifics are unlikely to be in a name, you could have names like "Missy Elliott".
I have a table with a column and value JobSkill = ".net sap lead". Now user enter the value "abap sap hana". I want to include a where condition which match exactly 3 or more continuous characters including space. In above scenario both have common "sap" substring so the condition should result in true. Below is my query. Please help. Previously I am using charindex but it does not resolve the purpose. I am using sql server 2008
SELECT Email_Id, JobSkill FROM Jobs
WHERE CHARINDEX(JobSkill, "abap sap hana") > 0
You need to create a function which loops through all positions of characters of String1 except the last 2, and check if String2 is like '%' + [(x,x+1,x+2)] + '%' string, where x is current position.
So for stings ('abcd acd g', 'ert acd'),
it should check
'ert acd' like '%abc%'
'ert acd' like '%bcd%'
'ert acd' like '%cd %'
'ert acd' like '%d a%'
and so on...
If like returns TRUE, break the loop.
Try like this,
SELECT j.Email_Id
,j.JobSkill
FROM Jobs j
INNER JOIN (
SELECT LTRIM(RTRIM(m.n.value('.[1]', 'varchar(8000)'))) SearchString
FROM (
SELECT CAST('<XMLRoot><RowData>' + REPLACE(#Input, ' ', '</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
) t
CROSS APPLY x.nodes('/XMLRoot/RowData') m(n)
) T ON j.JobSkill LIKE '%' + T.SearchString + '%'