SQL syntax error
Posted
by Robert
on Stack Overflow
See other posts from Stack Overflow
or by Robert
Published on 2010-06-08T10:17:34Z
Indexed on
2010/06/08
10:22 UTC
Read the original article
Hit count: 327
sql
Im using Microsoft SQL Server which I think is T-SQL or ANSI SQL.
I want to search a database with a string. The matches that fit the begging of the string should come first then sort alphabetically.
I.e. If the table contains FOO
, BAR
and RAP
a search for the string 'R'
should yield:
RAP
BAR
In that order.
Here is my attempt:
SELECT Name
FROM MyTable
WHERE (Name LIKE '%' + @name + '%')
ORDER BY (IF(Name LIKE @name + '%',1,0))
The error message is: "must declare scalar variable @name"
© Stack Overflow or respective owner