Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIKE with integers, in SQL

Tags:

Can I replace the = statement with the LIKE one for the integers ?

by eg. are the following the same thing:

select * from FOOS where FOOID like 2     // and  select * from FOOS where FOOID = 2 

I'd prefer to use LIKE instead of = because I could use % when I have no filter for FOOID...

SQL Server 2005.

EDIT 1 @Martin
enter image description here

like image 930
serhio Avatar asked Apr 22 '11 11:04

serhio


People also ask

Can we use like with integer in SQL?

LIKE is a string operator and has nothing to do with integers.

How use like in SQL with numbers?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can you apply like operator on integer value explain with example?

You can change your Field PhoneNumbers and store as String and then use the Like You can alter your table so that you can use the LIKE statement, if you still want to use BIGint for your phone numbers, you cannot get the exact Phone Number without using = the method you can use is Between method that looks for the ...


1 Answers

select * from FOOS where FOOID like 2 

should be avoided as it will cause both sides to be implicitly cast as varchar and mean that an index cannot be used to satisfy the query.

CREATE  TABLE #FOOS ( FOOID INT PRIMARY KEY, Filler CHAR(1000) ) INSERT INTO #FOOS(FOOID) SELECT DISTINCT number  FROM master..spt_values   SELECT * FROM #FOOS WHERE FOOID LIKE 2  SELECT * FROM #FOOS WHERE FOOID = 2  DROP TABLE #FOOS 

Plans (notice the estimated costs)

enter image description here

Another way of seeing the difference in costs is to add SET STATISTICS IO ON

You see that the first version returns something like

Table '#FOOS__000000000015'. Scan count 1, logical reads 310 

The second version returns

Table '#FOOS__000000000015'. Scan count 0, logical reads 2 

This is beacuse the reads required for the seek on this index are proportional to the index depth whereas the reads required for the scan are proportional to the number of pages in the index. The bigger the table gets the larger the discrepancy between these 2 numbers will become. You can see both of these figures by running the following.

SELECT index_depth, page_count FROM sys.dm_db_index_physical_stats (2,object_id('tempdb..#FOOS'), DEFAULT,DEFAULT, DEFAULT) WHERE object_id = object_id('tempdb..#FOOS') /*In case it hasn't been created yet*/ 
like image 148
Martin Smith Avatar answered Oct 11 '22 14:10

Martin Smith