Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Server Collation (ASCII Table sort and Case Insensitive/Accent Insensitive) Expand / Collapse
Author
Message
Posted Thursday, January 19, 2012 2:57 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 26, 2012 8:52 PM
Points: 10, Visits: 30
Hi everybody ,

I have an issue about SQL Server Collations.

Our owned programmed database use Case-Insensitive/Accent Insensitive data ordered like ASCII characters table.

Here is this sort order :
32 (space)
! 33
" 34
# 35
$ 36
%37
& 38
’ 39
( 40
) 41
*42
+43
, 44
- 45
. 46
/ 47
0 48
...(Others numbers from 49 to 57)
: 58
; 59
<60
=61
>62
?63
@64
A 65
...(others UPPER LETTERS From 66 to 90)
[ 91
\ 92
...
a 97;
...(others LOWER LETTERS FROM 98 TO 122)
...(others symbols)
à 224;
...OTHERS LETTERS WITH ACCENTS
...(others symbols)


You can look at http://www.asciitable.com/

My issue: I have to find a way to do the same in SQL Server (a collation that respects the begin of ASCII table sort with CASE INSENSITIVE/ACCENT INSENSITIVE).
Then, the result will be like:
! , ", #, $, %, &, ’, ..., 0,1,2, 3, .... A, a, à, O, o, ô, E, e, é, è, ê, .....

I have made many searches and I found that binaries collations respect ASCII table sort order but they do not support CASE INSENSITIVE/ACCENT INSENSITIVE options. However, non binaries collations support CASE INSENSITIVE/ACCENT INSENSITIVE but do not respect ASCII table sort order

I tried without success :
DataBase Collation = CollationXXX_BIN with SELECT column with COLLATE CollationXXX_CI_AI
DataBase Collation = CollationXXX_CI_AI with SELECT column with COLLATE CollationXXX_BIN

Is there a way that could solve my problem ? A way to create my own collation in SQL Server ?

Thank you very much

PS: I don't speak english very well but i Hope you'll understand
Post #1239099
Posted Friday, January 20, 2012 12:49 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:29 PM
Points: 11,645, Visits: 27,738
remember what case-Insensitive collations are used for: if i search for "apple' or "Apple" or "APPLE", i'd get results. that's perfect. now if i want to ORDER those results, i can order by a binary sensitive collation; i think that's the thing you are missing, so there's no need to come up with your own collation.

so the thing to remember is you can order your data differently than the collation it is stored in.

SELECT 
YourColumn,
OtherData
FROM YourTable
WHERE YourColumn = 'Apple'
ORDER BY YourColumn Collate SQL_Latin1_General_CP850_BIN



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1239217
Posted Friday, January 20, 2012 7:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 26, 2012 8:52 PM
Points: 10, Visits: 30
Thank you Lowell for your answer .

I tried what you said:
Database Collation CI_AI with a select column collate with a collation BIN.

More explanations:
With my select ordered by column with a binay collation as you suggest, it wll return

1- !apple
2- -apple
3- 0apple
4- 1apple
5- APPLE
6- Apple
7- Bpple
8- Zpple
9- apple
10-bpple
11-zpple
12-àpple
13-èpple
etc...

But I want

1- !apple
2- -apple
3- 0apple
4- 1apple
5- APPLE
6- Apple
7- apple
8-àpple
9- Bpple
10-bpple
11-èpple
12- Zpple
13-zpple

Thank you.
Post #1239466
Posted Friday, January 20, 2012 9:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:29 PM
Points: 11,645, Visits: 27,738
well i couldn't figure out what order you wanted via your example;
lets switch to real code so we can figure it out better.

does this do what you want? what's wrong witht he order in this example? you'll need to explain why certain things need to go first vs second...that's what is missing for me, i think; i can't get a handle on the order by criteria.

;With MySampleData AS (
SELECT N,
CHAR(MiniTally.N) + 'pple' As Word
FROM (SELECT
ROW_NUMBER() OVER (ORDER BY name) AS N
FROM sys.columns)MiniTally
WHERE N BETWEEN 32 AND 255
)
SELECT *
from MySampleData
ORDER BY Word,Word Collate SQL_Latin1_General_CP850_BIN




Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1239600
Posted Sunday, January 22, 2012 8:36 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:48 PM
Points: 10,989, Visits: 10,536
One of the SQL collations should do what you need. This one works for your sample data:

DECLARE @Apples TABLE
(
sequence integer NOT NULL,
apple varchar(6) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL
)

INSERT @Apples
(sequence, apple)
VALUES
(1, '!apple'),
(2, '-apple'),
(3, '0apple'),
(4, '1apple'),
(5, 'APPLE'),
(6, 'Apple'),
(7, 'apple'),
(8, 'àpple'),
(9, 'Bpple'),
(10, 'bpple'),
(11, 'èpple'),
(12, 'Zpple'),
(13, 'zpple')

SELECT * 
FROM @Apples AS a
ORDER BY apple;

-- SQL Server Sort Order 54 on Code Page 1252 for non-Unicode Data
SELECT *
FROM sys.fn_helpcollations() AS fh
WHERE fh.name = 'SQL_Latin1_General_CP1_CI_AI';





Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1239984
Posted Tuesday, January 24, 2012 8:34 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 26, 2012 8:52 PM
Points: 10, Visits: 30
Hi
Thank you Lowell and SQL Kiwi for your answers.

Our application send SQL requests one-by-one via ODBC cursor and then fetch next. With the good collation, SQL would return the good one first result and then the fetch next would return the next ones in order.

I've tried SQL Kiwi solution and it doesn't work

USE [TestCollation
GO
/****** Object: Table [dbo].[tmp] Script Date: 01/24/2012 09:48:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tmp](
[char_name] [varchar](1) COLLATE SQL_Latin1_General_CP1_CI_AI NOT NULL ,
[ascii_val] [smallint] NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


INSERT INTO [TestCollationFrench].[dbo].[tmp]
([char_name]
,[ascii_val])
VALUES
(<char_name, varchar(1),>
,<ascii_val, smallint,>)

First print screen: initial data

Second print screen :SELECT * FROM dbo.tmp ORDER BY char_name. You'll notice that the results are not ordered by the column ascii_val order. Only the 16 firt lines are correctly ordered.

Third print screen: This is what i want to obtain. Results expected (almost ordered by ascii_val column with numbers before letters).

Have a nice day :)











  Post Attachments 
1.jpg (9 views, 60.00 KB)
2.jpg (13 views, 68.55 KB)
3.jpg (12 views, 67.58 KB)
Post #1240928
Posted Tuesday, January 24, 2012 9:17 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:48 PM
Points: 10,989, Visits: 10,536
diallonina (1/24/2012)
I've tried SQL Kiwi solution and it doesn't work

It worked for the sample data you provided

Look, images are not easy for us to work with - if you want a definitive answer, provide test data in the form I did: CREATE TABLE with INSERT statements. From what you have said, it might be worth looking trying the SQL_Latin1_General_CP437_CI_AI collation, but I am not about to transcribe images into test data for you to test that out.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1240973
Posted Tuesday, January 24, 2012 12:42 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, March 26, 2012 8:52 PM
Points: 10, Visits: 30
Ok, I understand SQL Kiwi. I'll make insert statements and post them after.

Thank you
Post #1241134
Posted Tuesday, January 08, 2013 3:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 08, 2013 3:23 PM
Points: 1, Visits: 2
Hello,

I believe I had the same need/problem and I found a solution using:

select * from myTable order by UPPER(myField) COLLATE SQL_Latin1_General_CP850_BIN

the trick of course is to use UPPER to achieve case-insensitivity and then to use a binary collation to get everything else in an ASCII order.

Cheers,
Post #1404467
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse