I have a table called TEMP and I have inserted values as shown below. We can see word 'Dhinakaran' is written in different formats as 4 records here.
If I wanted to select only 'dhinakaran', (3rd record here), Then my Query
as by default while installing, SQL Server will take sentences as Case Insensitive.
To Know which column of is implemented with COLLATE function, We can run The following Stored Procedure.
CREATE TABLE TEMP (ID INT, NAME VARCHAR(100))
INSERT INTO TEMP (ID, NAME ) VALUES
(1,'DHINAKARAN'),
(2,'dhINAKaRAN'),
(3,'dhinakaran'),
(4,'DHinakaRAN')
SELECT * FROMTEMP
If I wanted to select only 'dhinakaran', (3rd record here), Then my Query
SELECT * FROMTEMP WHERE NAME='dhinakaran' will fail to return 3rd row. The output of the actual query will be
as by default while installing, SQL Server will take sentences as Case Insensitive.
So we can overcome this problem by using COLLATE. COLLATE is implemented as follows in our Select Statement.
SELECT * FROM TEMP WHERE NAME COLLATE Latin1_General_CS_AS='dhinakaran'
Adding COLLATE Latin1_General_CS_AS makes the search case sensitive.
If we need any column for any table case sensitive permanently, Then while creating a Table itself, we can give COLLATE.
EX: We could have created a table like
CREATE TABLE TEMP
(ID INT,
NAME VARCHAR(100) COLLATE Latin1_General_CS_AS).
We can also Alter table with COLLATE and example is shown below:
ALTER TABLE TEMP
ALTER COLUMN NAME VARCHAR (100)
COLLATE LATIN1_GENERAL_CS_AS
To Know which column of is implemented with COLLATE function, We can run The following Stored Procedure.
EXEC sp_help TABLENAME
Comments
Post a Comment