SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SOLVED: LIKE operator ([ ] and/or %)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mallorz
Starting Member

11 Posts

Posted - 09/26/2012 :  15:25:20  Show Profile  Reply with Quote
This works:

SELECT emp_fname, emp_lname
FROM Employee
ORDER BY emp_lname ASC;

This does not:

SELECT emp_fname, emp_lname
FROM Employee
WHERE emp_lname LIKE '[H]%'
ORDER BY emp_lname ASC;

(nor does..LIKE 'H%')

Normally when something like this happens, its a punctuation thing. However, I went back and compared this to my textbook and it seems right on. What am I missing?

Edited by - mallorz on 09/26/2012 19:30:16

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/26/2012 :  15:44:00  Show Profile  Reply with Quote
What did you mean when you said it did not work? I tried the following examples, and they all work as expected/documented.
CREATE TABLE #tmp (emp_lname VARCHAR(32));
INSERT INTO #tmp VALUES ('Hiller'),('Brock'),('[H]older');

SELECT * FROM #tmp WHERE emp_lname LIKE 'H%'   -- returns Hiller
SELECT * FROM #tmp WHERE emp_lname LIKE '[H]%' -- returns Hiller
SELECT * FROM #tmp WHERE emp_lname LIKE '[[]%' -- returns [Holder]
SELECT * FROM #tmp WHERE emp_lname LIKE '[H[]%'-- returns Hiller and [H]older

DROP TABLE #tmp;
Go to Top of Page

bitsmed
Constraint Violating Yak Guru

276 Posts

Posted - 09/26/2012 :  16:18:02  Show Profile  Reply with Quote
Like sunitabeck wrote, the syntax seems ok, so please post the error you get.
Go to Top of Page

mallorz
Starting Member

11 Posts

Posted - 09/26/2012 :  17:09:32  Show Profile  Reply with Quote
The first example posted returns all names, in order of last name.

The second one should return all names that have a last name beginning with H. Instead, it returns an empty table. It does not actually give me an error.

Edited by - mallorz on 09/26/2012 17:18:49
Go to Top of Page

bitsmed
Constraint Violating Yak Guru

276 Posts

Posted - 09/26/2012 :  17:23:30  Show Profile  Reply with Quote
As the syntax is ok, two things comes to mind:

- Are you selecting from the right database?
- Do you receive an timeout?
Go to Top of Page

mallorz
Starting Member

11 Posts

Posted - 09/26/2012 :  18:57:21  Show Profile  Reply with Quote
quote:
Originally posted by bitsmed

As the syntax is ok, two things comes to mind:

- Are you selecting from the right database?
- Do you receive an timeout?



1. Yes, I have checked that (and when I simply delete the WHERE it works)
2. No, I do not receive any error or indication that something is wrong besides the fact that this returns no values.
Go to Top of Page

mallorz
Starting Member

11 Posts

Posted - 09/26/2012 :  19:29:38  Show Profile  Reply with Quote
I got it. I had to take out the % and use *

Access does not support %.

Edited by - mallorz on 09/26/2012 19:35:48
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.27 seconds. Powered By: Snitz Forums 2000