Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best FUNCTION to search for a character inside a string sql? [closed]

What is the best FUNCTION to search for a character inside a string sql?

I've been trying to search if there exists a character inside a fixed string.

For example I have:

1OF2. 040713 08:07 AM

And I want to know if there is a '.' inside the string.

like image 611
ajdeguzman Avatar asked Jul 04 '26 22:07

ajdeguzman


2 Answers

Use like:

select col like '%.%'

It is standard SQL and SQL Server has some good optimizations built into the query engine to handle it.

EDIT (in response to a comment):

Getting the first part of the string is a bit different. In that case:

select (case when col like '%.%' then left(col, charindex('.', col) - 1)
             else col
        end)
like image 153
Gordon Linoff Avatar answered Jul 07 '26 12:07

Gordon Linoff


CHARINDEX is also available. Use CHARINDEX and compare against 0 (not found). For example:

SELECT *
WHEN CHARINDEX('.',field) > 0
like image 45
Hogan Avatar answered Jul 07 '26 11:07

Hogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!