SQL select using condition that a column starts with a certain string - sql

I am using sql.
How to select all items from a table.
where a column (varchar) path is start with a certain string : "stringABC"
select * from ATable where path like 'stringABC';
The above sql is wrong, can any one help me to fix it?
Many thanks!

select *
from ATable
where path like 'stringABC%'

select * from ATAble where path like 'string%'

Related

SQL find word in string

I have a string like that format:
'%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%'
and i need to know is this string contains a word from column mName in table1:
In this case I need the output to be galaxy
I do not know what database do you use but this will work in Mysql and Oracle:
select *
from table1
where instr('%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%', mname) <> 0;
I have created a table called test
You use instr function to find a value from a column mName in the string if it exists then you show it.
Here is a DEMO
Or as forpas suggested in his comment you can use like:
select * from table1
where '%asdasasd\ngalaxy\n4198461841\nasdadadqd\n11111118181gals%'
like '%'||mname||'%'

SQL SELECT statement with a field's value from a value list

I need to query a SQLite database for some entries containing a field the value of which can be one of a defined list: 'Token1', 'Token2', ..., 'TokenN' - potentially a long one.
The straightforward SELECT statement would be something like
SELECT * FROM `my_table` WHERE `token` = 'Token1' OR `token` = 'Token2' OR ...
- far from elegant. I wonder, is there a better way to formulate the statement?
You can use IN Clause
SELECT * FROM `my_table`
WHERE `token` in ('Token1','Token2', 'Token3', ....);
Use the wildcard character %%.
SELECT * FROM `my_table` WHERE `token` like '%Token%'
If your defined list is or can be placed in a table, you can do the following:
SELECT
*
FROM [employeeName] Where dept In (Select dept From #tbl)

Using CONTAINS to find items IN a table

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.

Sql query to find records with column value is blank

I'm trying to find records with Column value is blank. as you can see in the table I have records with following values when I fire a Distinct query on the Column MOVE_STU.
now I can find all the record with column value related to (1,2,3,4 and 6) but I'm not able to find the records with Column Value related to (5). as there are Thousands of record in the table i'm not able to figure out how should I write query in order to get these records. Kindly help. Thanks in Advance. :)
Use trim and comparison with empty string to get records that contain only whitespaces:
SELECT *
FROM your_table
WHERE LTRIM(RTRIM(MOVE_STU)) = ''
Try something like this:
SELECT * FROM YourTable WHERE LTRIM(RTRIM(YourField)) = ''
This will give you all matches that are empty, or have only whitespace.
How about this ?
SELECT * FROM YourTable WHERE MOVE_STU = ''
SELECT * FROM YourTable WHERE ISNULL(MOVE_STU,'') = ''
You can try this:
If your datatype is int then you have to change in varchar
ALTER TABLE [tbl_name] ALTER COLUMN [MOVE_STU] [VARCHAR(50)];
then run the query:
SELECT * FROM [tbl_name] WHERE MOVE_STU IS NULL;

sql server matching field containing "$$" and "%"

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'