SQL Like Search String Starts With

SQL like search string starts with

SELECT * from games WHERE (lower(title) LIKE 'age of empires III');

The above query doesn't return any rows because you're looking for 'age of empires III' exact string which doesn't exists in any rows.

So in order to match with this string with different string which has 'age of empires' as substring you need to use '%your string goes here%'

More on mysql string comparision

You need to try this

SELECT * from games WHERE (lower(title) LIKE '%age of empires III%');

In Like '%age of empires III%' this will search for any matching substring in your rows, and it will show in results.

Search in SQL where string starts with X

Simply remove the first wildcard (%):

SELECT * FROM info WHERE name LIKE 'X%' LIMIT 10

Check string starts with specific letter

You want to prioritize the values being returned. Because you want only one, you can do this with ORDER BY:

SELECT Top 1 LC_ID, COALESCE(LC_UD, 0) as Record 
FROM CONTRACT
WHERE LC_ID LIKE '[FG]%'
ORDER BY (CASE WHEN LC_ID = 'F01' THEN 1 ELSE 2 END);

Note: This assumes you are using SQL Server (based on the syntax).

SQL - Find all words that start with some phrase

Here's a quick and dirty trick: A word begins with dan can either be at the beginning of a header or after a space, so:

SELECT * FROM books WHERE header LIKE 'dan%' OR header like '% dan%';

SQL startswith (using `LIKE`) on an expression

In standard SQL, you can also say:

where position(post.path in child.path) = 1

I don't know if your RDBMS supports that. PostgreSQL does.

How to find if a string starts with a number and a bracket in SQL server?

SQL Server doesn't have great pattern matching but if you know that the numbers prefixing the ) will always be less then 500, you can do this:

DECLARE @myString VARCHAR(100) = '266) Some text'

If (PATINDEX('[0-9])%', @myString) > 0) OR --Check for one digit
(PATINDEX('[0-9][0-9])%', @myString) > 0) OR --Check for two digits
(PATINDEX('[0-9][0-9][0-9])%', @myString) > 0) --Check for three digits
begin
print @myString
end

SQL SELECT WHERE field contains words

Rather slow, but working method to include any of words:

SELECT * FROM mytable
WHERE column1 LIKE '%word1%'
OR column1 LIKE '%word2%'
OR column1 LIKE '%word3%'

If you need all words to be present, use this:

SELECT * FROM mytable
WHERE column1 LIKE '%word1%'
AND column1 LIKE '%word2%'
AND column1 LIKE '%word3%'

If you want something faster, you need to look into full text search, and this is very specific for each database type.

determine if column value string starts with a number

SELECT CASE WHEN ISNUMERIC(SUBSTRING(LTRIM(Description), 1, 1)) = 1 
THEN 'yes'
ELSE 'no'
END AS StartsWithNumber
FROM Questions
  • ISNUMERIC
  • SUBSTRING

SQL Server starts with list of strings

You can use a combination of left, or, and in to shorten your query.

WHERE left(site_id,2) in ( 'AB', 'BC', 'AO', 'BO', 'CA') or left(site_id, 3) = 'PAF' or left(site_id, 1) = 'Z'

Is there StartsWith or Contains in t sql with variables?

StartsWith

a) left(@edition, 15) = 'Express Edition'
b) charindex('Express Edition', @edition) = 1

Contains

charindex('Express Edition', @edition) >= 1

Examples

left function

set @isExpress = case when left(@edition, 15) = 'Express Edition' then 1 else 0 end

iif function (starting with SQL Server 2012)

set @isExpress = iif(left(@edition, 15) = 'Express Edition', 1, 0);

charindex function

set @isExpress = iif(charindex('Express Edition', @edition) = 1, 1, 0);


Related Topics



Leave a reply



Submit