Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to compare variable to string? Expand / Collapse
Author
Message
Posted Monday, October 21, 2013 5:21 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, February 17, 2014 10:08 PM
Points: 271, Visits: 728
When I try to compare the value of a column to a value represented by a variable using LIKE

EG. where computer_name LIKE ''' & '%' & @realname & '%' & '''

I get error:

--ERROR message
Msg 402, Level 16, State 1, Line 7
The data types varchar and varchar are incompatible in the modulo operator.

I need the modulo to have fuzzy lookup. How to do?

--example
DECLARE @REALNAME varchar(255)
SET @REALNAME = (select top(1) realname from locations where realname like '%ricki ricardo%')

select
computer_name
from computers
where computer_name like ''' + '%' + @realname + '%' + '''
Post #1506924
Posted Monday, October 21, 2013 6:19 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:31 AM
Points: 3,590, Visits: 5,097
Try changing your & to +


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1506933
Posted Monday, October 21, 2013 6:25 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, February 17, 2014 10:08 PM
Points: 271, Visits: 728
sorry, did that. I showed that in second example, but forgot to edit in the first.
Post #1506935
Posted Monday, October 21, 2013 6:38 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:31 AM
Points: 3,590, Visits: 5,097
Firstly, to avoid confusion, % in the way you are using it is not the modulo operator, it is a search wildcard character.

Secondly, I think what you may be trying to do is much harder than you think.

I recall an article about a year back on fuzzy searching. You might want to take a look at that if you can find it. Try a search on "Fuzzy" within articles - a fair number turn up. One of them might help you.

It might also be helpful if you posted some DDL and sample data to show what kind of hits you're wanting to find.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1506940
Posted Tuesday, October 22, 2013 11:21 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, February 17, 2014 10:08 PM
Points: 271, Visits: 728
OK, I found an entirely different approach to a problem at hand, which eliminates need to compare variable with string.
I appreciate it (as always/over the top!)
Post #1507254
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse