59

I have this table in SQL Server 2012:

Id INT 
DomainName NVARCHAR(150)

And the table have these DomainName values

  1. google.com
  2. microsoft.com
  3. othersite.com

And this value:

mail.othersite.com

and I need to select the rows where the string ends with the column value, for this value I need to get the row no.3 othersite.com

It's something like this:

DomainName Like '%value' 

but in reverse ...

'value' Like %DomainName

4 Answers 4

104

You can use such query:

SELECT * FROM TABLE1
WHERE 'value' LIKE '%' + DomainName
Sign up to request clarification or add additional context in comments.

1 Comment

Note that on other databases (eg Oracle) you'll need to use the string concatenation operator, because '+' will give you an invalid number error. E.g: WHERE 'value' LIKE '%' || DomainName
1

It works on mysql server. [ LIKE '%' || value ] not works great because when value start with numbers this like not return true.

SELECT * FROM TABLE1 WHERE DomainName like CONCAT('%', value)

Comments

-1

In Oracle you can use REGEXP_LIKE string function for string start/ends with search. This function 1st argument is an expression/source string and 2nd argument is a pattern/search string. Use dollar $ for a ends with value and ^ for starts with value. For example

select * from TABLE1 where REGEXP_LIKE(DomainName, :value ||'$');

If you have a OR condition between different values, then use following pipe | syntax.

select * from TABLE1 where REGEXP_LIKE(DomainName, 'value1$|value2$|value2$');

Refer this link for more info on REGEXP_LIKE string function

Comments

-1

one possible answer could be

SELECT patient_id,first_name from patients where first_name like 'S%s'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.