Solved

SQL Filter Question

Posted on 2016-09-16
8
90 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 48

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 28

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 28

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 28

Assisted Solution

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

749 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