-1

This query returns the following:

select * 
from dbo.persons 
where LastName like '%Dan%';

Results:

enter image description here

How can I select only the first row with LastName = "Dan" and not "Dant" or "Dan12" ?

I am using the following query:

select * 
from dbo.persons 
where LastName like '%[^a-z]Dan[^a-z]%' 
   or LastName like 'Dan[^a-z]%' 
   or LastName like '%[^a-z]Dan'

which returns the following:

LastName = Dan12
LastName = pine and Dan and apple

I want only these results:

LastName = Dan
LastName = pine and Dan and apple

I tried the following query but it returned nothing:

select * 
from dbo.persons 
where LastName like '%[^a-z][^0-9]Dan[^a-z][^0-9]%' 
   or LastName like 'Dan[^a-z][^0-9]%' 
   or LastName like '%[^a-z][^0-9]Dan'
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ilak
  • 158
  • 3
  • 13

5 Answers5

2

Simply

select * from dbo.persons where LastName ='Dan';
Ilyes
  • 14,640
  • 4
  • 29
  • 55
  • 1
    This solution does not work. It matches 'Dan ' (extra space at the end) also. – The Impaler Jun 20 '18 at 14:24
  • @TheImpaler You are very wrong here. `=` wont match any extra space. And the weird part is you also have the exact same query but you said Sami is the one wrong? – Juan Carlos Oropeza Jun 20 '18 at 15:04
  • 2
    I can't believe what you are saying. Am I going crazy here? :D I have SQL Server 2014. I just tried and equals (=) return the 'Dan ' (extra space at the end). For the record, I tried to down vote my answer, but doesn't allow me to. What version of SQL Server are you using? Is your column `char` or `varchar`? Mine is `varchar`. – The Impaler Jun 20 '18 at 19:25
  • @TheImpaler extra spaces are ignored in equality operators in SQL Server so you aren't crazy. – S3S Jun 22 '18 at 19:12
2

Both of the statement below will do what you want:

select * from dbo.persons where LastName like 'Dan';

or:

select * from dbo.persons where LastName = 'Dan';
The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • I'd arguably state the second of these two is better practice, as it's a bit more direct on exactly what you're looking to pull back. – user2366842 Jun 20 '18 at 13:48
  • for the sake of the argument, the second will return a row with value 'Dan ', while the first will not :) – George Menoutis Jun 20 '18 at 13:58
  • @GeorgeMenoutis I just tried both of them against 'Dan ' (extra space at the end), and to my surprise BOTH find it. I can't believe even equals (=) also matches the `varchar` column. Is this a SQL Server bug? – The Impaler Jun 20 '18 at 14:23
  • This is strange, are you sure? To my knowledge, = disregards trailing spaces, but LIKE doesn't. – George Menoutis Jun 20 '18 at 14:30
  • Just tried both (like and equals). I tried with extra spaces at the beginning, at the end, and both places. The bug is for spaces at the end only. Please downvote my answer, and upvote the question. – The Impaler Jun 20 '18 at 14:31
1

I used the following query that seems to work:

  select * from dbo.persons where LastName like '%[^a-z]Dan[^a-z]%' OR 
                                  LastName like 'Dan[^a-z0-9]%' OR 
                                  LastName like '%[^a-z]Dan' OR
                                  LastName like 'Dan';

These are rows that are returned which are expected.

enter image description here

Ilak
  • 158
  • 3
  • 13
  • Thanks to the following post:https://stackoverflow.com/questions/5444300/search-for-whole-word-match-with-sql-server-like-pattern This post helped me in framing this query. – Ilak Jun 20 '18 at 15:41
  • You can mark this answer as correct so everyone know this is already solved. – Juan Carlos Oropeza Jun 21 '18 at 15:11
  • @JuanCarlosOropeza I have to wait for another day to accept this answer. – Ilak Jun 21 '18 at 16:37
0

Im going to guess you dont know which one is the first one so this doesn't work:

where LastName = 'Dan'

instead you select the first one order alphabetical

select TOP 1 * 
from dbo.persons 
where LastName like '%Dan%'
ORDER BY LastName ;
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
0

Instead of using LIKE which check any occurrence of a character or a set of characters by using the wildcard % use = to get the exact match you need. If there will be a trailing whitespace at the beginning or at the end it will not get the exact match so use LTRIM() or RTRIM() to get rid of the whitespaces.

SELECT * FROM dbo.persons WHERE LastName = LTRIM(' Dan')

OR

 SELECT * FROM dbo.persons WHERE LastName = RTRIM('Dan ')
aiai
  • 1,129
  • 2
  • 11
  • 21
  • This solution does not work. It matches 'Dan ' (extra space at the end) also. – The Impaler Jun 20 '18 at 14:26
  • @TheImpaler i think it will not only work if there will be a space in the beginning , not at the end, if this will be the case then trim for the space at the beginning so it will still get the exact match – aiai Jun 20 '18 at 14:46