SQL Filter Question

I want to find out if there is a match, case notwithstanding for a first name, last name, gender.  It is my understanding that this will look at "Bob" and "BOB" as different names.  Yes?  If not please let know what a better approach for doing this would be.

            bPartFound = False
            Set rs = Server.CreateObject("ADODB.Recordset")
            sql = "SELECT RosterID FROM Roster WHERE FirstName = '" & field1 & "' AND LastName = '" & field2 & "' AND Gender = '" & field3
            sql = sql & "' AND TeamsID = " & lTeamID
            rs.Open sql, conn, 1, 2
            If rs.RecordCount > 0 Then bPartFound = True
            rs.Close
            Set rs = Nothing

Open in new window


Thank You!
Bob SchneiderCo-OwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
It depends on the collation setting for those columns.  There is a default collation for each db, but in SQL Server you can override that for a column.

This command will show you the collation for every column, along with some other things, which you can ignore :-).  If the collation has "_CS_" in it, it's case sensitive ('Bob' <> 'BOB'), if it has "_CI_" in it, it's not ('Bob' = 'BOB').

EXEC sys.sp_help  Roster
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
If needed you can control which collation is used at query time like this:
sql = "SELECT RosterID FROM Roster WHERE FirstName COLLATE Latin1_General_CI_AS = '" & field1 
& "' AND LastName COLLATE Latin1_General_CI_AS = '" & field2 
& "' AND Gender COLLATE Latin1_General_CI_AS = '" & field3

Open in new window

Using this approach you could make the search case insensitive or case sensitive at your command (by making the collation another variable)

DECLARE @roster TABLE
    ([FirstName] varchar(3), [LastName] varchar(5), [Gender] varchar(1))
;
INSERT INTO @roster
    ([FirstName], [LastName], [Gender])
VALUES
    ('BOb', 'Smith', 'm'),
    ('BOb', 'smith', 'M'),
    ('bob', 'Smith', 'm')
;

SELECT * FROM @Roster 
WHERE FirstName COLLATE Latin1_General_CI_AS = 'bob'
--OR LastName COLLATE Latin1_General_CI_AS = 'smith'
--OR Gender COLLATE Latin1_General_CI_AS = 'm'

SELECT * FROM @Roster 
WHERE FirstName COLLATE Latin1_General_CS_AS = 'bob'
--OR LastName COLLATE Latin1_General_CS_AS = 'smith'
--OR Gender COLLATE Latin1_General_CS_AS = 'm'

FirstName LastName Gender 
--------- -------- ------ 
BOb       Smith    m      
BOb       smith    M      
bob       Smith    m      

(3 row(s) returned)(3 row(s) affected)

FirstName LastName Gender 
--------- -------- ------ 
bob       Smith    m      

(1 row(s) returned)(3 row(s) affected)

Open in new window

0
Pawan KumarDatabase ExpertCommented:
Here you go !--

1. With default collation bob & Bob are same.

2. SQL's default collation is - Latin1_General_CI_AI. (SQL 2012)..

3. You can change collation at column level using command

--

CREATE TABLE dbo.MyTable  
  (PrimaryKey   int PRIMARY KEY,  
   CharCol      varchar(10) COLLATE French_CI_AS NOT NULL  
  );  
GO  
ALTER TABLE dbo.MyTable ALTER COLUMN CharCol  
            varchar(10)COLLATE Latin1_General_CI_AS NOT NULL;  
GO  

--

Open in new window

--

4. You can also apply collation in SQL Queries. E.g.

--

SELECT 
  'REVOKE ' + convert(varchar(50),x.[Action])  
+ ' on '    + x.[Schema]
+ '.'       + convert(varchar(50),x.[Object])  
+ ' TO '    + convert(varchar(50),x.[User]) COLLATE Latin1_General_CI_AS
FROM  (
SELECT 
  u.name  COLLATE Latin1_General_CI_AS AS 'User', 
  schema_name(o.schema_id)  As 'Schema',
  o.name  COLLATE Latin1_General_CI_AS AS 'Object' , 
  p.permission_name COLLATE Latin1_General_CI_AS AS 'Action' 
--into tmp
FROM sys.database_permissions p, sys.database_principals u, sys.all_objects o 
WHERE o.object_id = p.major_id
AND p.grantee_principal_id = u.principal_id
AND p.grantee_principal_id IN (0, 2)
) x 

--

Open in new window


5. You can also change collation database level using below command

--

USE master;  
GO  
IF DB_ID (N'MyOptionsTest') IS NOT NULL  
DROP DATABASE MyOptionsTest;  
GO  
CREATE DATABASE MyOptionsTest  
COLLATE Latin1_General_100_CS_AS_SC;  
GO  
  
--Verify the collation setting.  
SELECT name, collation_name  
FROM sys.databases  
WHERE name = N'MyOptionsTest';  
GO  

USE master;  
GO  
ALTER DATABASE MyOptionsTest  
COLLATE French_CI_AS ;  
GO  
  
--Verify the collation setting.  
SELECT name, collation_name  
FROM sys.databases  
WHERE name = N'MyOptionsTest';  
GO  

--

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bob SchneiderCo-OwnerAuthor Commented:
Wow.  This is awesome and I really appreciate the help.  So if the default is case insensitive then I should be fine as is, correct?  That is, if field1 is "Bob" and it finds "bob" it will treat them as equal.

How do I check what my collation is?  Can I do it in Enterprise Manager?
0
Pawan KumarDatabase ExpertCommented:
Right click on your Database and you will get.

Please refer the screen shot attached.
Collation.png
0
Pawan KumarDatabase ExpertCommented:
You can use SSMS to get collation
0
Pawan KumarDatabase ExpertCommented:
@Author - Do you need more help on this question ?
0
Scott PletcherSenior DBACommented:
The default collation is set during the SQL instance install.  Unless the installer specified otherwise, SQL will use a case insensitive collation.


How do I check what my collation is?
 

I provided the command line earlier:
EXEC sys.sp_help <table_name>
but ...

Can I do it in Enterprise Manager?

Yes ... mostly.  You can easily see the default collation for the db via SSMS ("Enterprise Mgr").  But not column by column.  While it's very rare for collation to be changed for just one column, it is possible.  

Therefore, I prefer to use the command that specifically shows each column, so you can be absolutely sure what that column's collation is.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.