Solved

SQL Filter Question

Posted on 2016-09-16
8
95 Views
Last Modified: 2016-10-02
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!
0
Comment
Question by:Bob Schneider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 41802311
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
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
ID: 41802459
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
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 125 total points
ID: 41802479
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:Bob Schneider
ID: 41803449
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
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 125 total points
ID: 41803461
Right click on your Database and you will get.

Please refer the screen shot attached.
Collation.png
0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 125 total points
ID: 41803462
You can use SSMS to get collation
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41803496
@Author - Do you need more help on this question ?
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 41805030
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

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question