Sorting/Compare Problem in SQL Server
-
Wednesday, February 29, 2012 1:26 AM
I'm having trouble figuring out how to run a query in SQL Server. I want to run a query and select columns from a table and add a where statement too.
So like: SELECT *
FROM Product
WHERE Name = 'Some Name';
The data type for Name is text, but when I try to run a query it says text data type can't be sorted or compared except when using IS NULL or LIKE.
This type of query works in Access, have I setup my table wrong in SQL Server or something? This type of query should work shouldn't it?
All Replies
-
Wednesday, February 29, 2012 2:29 AM
I'm having trouble figuring out how to run a query in SQL Server. I want to run a query and select columns from a table and add a where statement too.
So like: SELECT *
FROM Product
WHERE Name = 'Some Name';
The data type for Name is text, but when I try to run a query it says text data type can't be sorted or compared except when using IS NULL or LIKE.
This type of query works in Access, have I setup my table wrong in SQL Server or something? This type of query should work shouldn't it?
The text data type is deprecated. If you actual data length is under 8000 characters, you should instead use varchar(n), where n is the maximum length. If your data can exceed 8000 characters, use varchar(MAX) in SQL 2005 and later, which will allow normal compare operations.
With text, you can still use SUBSTRING for equality comparisions on the first 8000 characters:
SELECT * FROM dbo.Product WHERE SUBSTRING(Name,1,8000) = 'Some Name';
Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/
- Proposed As Answer by Brian TkatchMicrosoft Community Contributor, Editor Wednesday, February 29, 2012 1:31 PM
- Marked As Answer by Stephanie Lv Tuesday, March 06, 2012 6:00 AM
-
Wednesday, February 29, 2012 3:48 AMAnswerer
In addition
WHERE PATINDEX('%'+ 'Some Name'+'%', Name )>0
Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/

