I have a table with a field name.
For a given name like ABC I want to get the records with that name and the records which have an L appended at the end.
So for ABC I want all records with name either ABC or ABCL.
I tried getting the records using the following code but it doesn't work.
SELECT * FROM tbl
WHERE name like "ABC[|L]"
I am using TSQL.
How can pattern match these names?
Use this SQL:
SELECT * FROM tbl WHERE name IN ('ABC', 'ABCL')
If you are using this SQL within a Stored Procedure / Function, something like following would work. Assuming, you are passing in the value for name in a #name variable.
SELECT * FROM tbl WHERE name IN (#name, #name + 'L')
Use the IN operator.
SELECT *
FROM tbl
WHERE name IN ('ABC', 'ABCL')
In case you will be using variables instead of literal strings, try this:
select *
from tbl
where name like (#YourName + #ExtraLetter)
or name = #YourName
Related
How to make MS Query work with comma separated parameters in Excel cell?
My query is:
SELECT *
FROM ABC
WHERE Id in (?)
When I put id number for example "1" the query works, but I want to put into a parameters cell a few id's 1, 2, 3, 4 etc, but then I'm trying to to this the query doesn't work... How can I put parameter with comma separated values?
there is 2 diff way to do it:
select * from abc where id in ('1','2','3') etc but not in excel - maybe use notepad++
second way :)
select * from abc where (id like '1' or id like '2' or id like '3') etc
:)
You can use IN in you sql query.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (1,2,3,4);
also try to use BETWEEN with comma as parameters.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Following is the query to select column data from table, where column data starts with a OR b OR c. But the answer i am looking for is to Select data which starts with List of Strings.
SELECT * FROM Table WHERE Name LIKE '[abc]%'
But i want something like
SELECT * FROM Table WHERE Name LIKE '[ab,ac,ad,ae]%'
Can anybody suggest what is the best way of selecting column data which starts with list of String, I don't want to use OR operator, List of strings specifically.
The most general solution you would have to use is this:
SELECT *
FROM Table
WHERE Name LIKE 'ab%' OR Name LIKE 'ac%' OR Name LIKE 'ad%' OR Name LIKE 'ae%';
However, certain databases offer some regex support which you might be able to use. For example, in SQL Server you could write:
SELECT *
FROM Table
WHERE NAME LIKE 'a[bcde]%';
MySQL has a REGEXP operator which supports regex LIKE operations, and you could write:
SELECT *
FROM Table
WHERE NAME REGEXP '^a[bcde]';
Oracle and Postgres also have regex like support.
To add to Tim's answer, another approach could be to join your table with a sub-query of those values:
SELECT *
FROM mytable t
JOIN (SELECT 'ab' AS value
UNION ALL
SELECT 'ac'
UNION ALL
SELECT 'ad'
UNION ALL
SELECT 'ae') v ON t.vame LIKE v.value || '%'
I'm trying to write a SP that will allow users to search on multiple name strings, but supports LIKE functionality. For example, the user's input might be a string 'Scorsese, Kaurismaki, Tarkovsky'. I use a split function to turn that string into a table var, with one column, as follows:
part
------
Scorsese
Kaurismaki
Tarkovsky
Then, normally I would return any values from my table matching any of these values in my table var, with an IN statement:
select * from myTable where lastName IN (select * from #myTableVar)
However, this only returns exact matches, and I need to return partial matches. I'm looking for something like this, but that would actually compile:
select * from myTable where CONTAINS(lastName, select * from #myTableVar)
I've found other questions where it's made clear that you can't combine LIKE and IN, and it's recommended to use CONTAINS. My specific question is, is it possible to combine CONTAINS with a table list of values, as above? If so, what would that syntax look like? If not, any other workarounds to achieve my goal?
I'm using SQL Server 2016, if it makes any difference.
You can use EXISTS
SELECT * FROM myTable M
WHERE
EXISTS( SELECT * FROM #myTableVar V WHERE M.lastName like '%'+ V.part +'%' )
Can your parser built the entire statement? Will that get you what you want?
select *
from myTable
where CONTAINS
(lastName,
'"Scorsese" OR "Kaurismaki" OR "Tarkovsky"'
)
This can be done using CHARINDEX function combined with EXISTS:
select *
from myTable mt
where exists(select 1 from #myTableVar
where charindex(mt.lastName, part) > 0
or charindex(part, mt.lastName) > 0)
You might want to omit one of the conditions in the inner query, but I think this is what you want.
One of my column in sql table contain values like
$$ADC.ES%32,A
How can I match this in my select where clause?
Select .... From .... Where ColumnName = '$$ADC.ES%32,A'
Thanks
it should find with your query any way try this
select * from table where name like '$$ADC.ES%32,A'
I have one database table name test123 and having column name. And it contains the data like 'nir,kal,man' Now, when i am querying the table with select statement as per below :
select * from test123 where name = 'nir,kal,man';
But this will not return any result...Why this happened.? How i have to write query so that will return the result?
I am using Sql server 2008.
Thanks...!
= operator returns exact match, so if your cell contain data "like" that you need to use LIKE operator:
select * from test123 where name like '%nir,kal,man%'
where % will be replaced with any set of characters.
Also check that you're targeting correct database by using full name
select * from yourdb.dbo.test123 where....
if Nir is in first row Kal in 2nd row and man is in 3rd row then you should write query like this
select * from test123 where name in ('nir','kal','man')