Solved

SQL Filter Question

Posted on 2016-09-16
8
68 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
8 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher 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 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal 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 17

Assisted Solution

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

Expert Comment

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

Assisted Solution

by:ScottPletcher
ScottPletcher 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now