Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Filter Question

Posted on 2016-09-16
8
Medium Priority
?
104 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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 500 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 35

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 35

Assisted Solution

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

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

Assisted Solution

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

Expert Comment

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

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

927 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