0

I know this is a simple question, but I can't get it to work.

This is my query in my SqlCommand:

SELECT * FROM zipcode  WHERE city LIKE @prefixtext + '%' ;

I only want 10 results, every other answer suggests

SELECT TOP 10 * FROM zipcode  WHERE city LIKE @prefixtext + '%' ;
SELECT * FROM zipcode  WHERE city LIKE @prefixtext + '%'  LIMIT 10 ;

both do not work

12
  • I'm likely wrong on this since I'm not very familiar with SQL Server, but I don't think that + is concatenation. Commented Jun 7, 2012 at 2:55
  • Is zipcode really your table name, or the name of the column you want to retrieve? Basic query format is SELECT column_name (or * for all columns) ` FROM table_name WHERE ...` Commented Jun 7, 2012 at 2:55
  • 5
    TOP 10 is SQL Server specific. LIMIT 10 works on MySQL (maybe also Oracle?). Which DB are you using? What error or incorrect result do you get? Commented Jun 7, 2012 at 2:58
  • 3
    SELECT TOP 10 * FROM zipcode WHERE city LIKE @prefixtext + '%' would definitely work in MSSQL, limit 10 is not MSSQL keyword. What is the error here? Commented Jun 7, 2012 at 3:01
  • 1
    Could you show the code where you populate the SqlCommand object? Commented Jun 7, 2012 at 3:11

3 Answers 3

2

I believe these are all correct.

Oracle:

select * from zipcode where city like @prefixtext + '%' and rownum <=10

SQL Server/Sybase:

select top 10 * from zipcode where city like @prefixtext + '%'

DB2/PostgreSQL:

select * from zipcode where city like @prefixtext || '%' fetch first 10 rows only

MySQL:

select * from zipcode where city like @prefixtext + '%' limit 10
Sign up to request clarification or add additional context in comments.

Comments

0
declare @like varchar(50)

set @like = @prefixtext + '%';

SELECT TOP 10 * FROM zipcode  WHERE city LIKE @like

Comments

-1
Select * from zipcode where city like @prefixtext + '%'

1 Comment

Does not keep to " I only want 10 results"

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.