SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


character ordering


character ordering

Author
Message
paul.knibbs
paul.knibbs
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2523 Visits: 6232
mtassin (11/1/2012)
Oh and of course I got it wrong... because I just went with

SELECT ASCII('Z')-ASCII('0')




That's pretty much what I did, but I did it in my head rather than using code. I was 100% sure this would be the wrong answer, mind you, I was just curious to see what the right one was! :-)
Kick6Tiger
Kick6Tiger
Say Hey Kid
Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)Say Hey Kid (713 reputation)

Group: General Forum Members
Points: 713 Visits: 766
I was dumbfounded after the correct answer was not listed: 11. The question clearly states "How many 8-bit characters are there between '0' and 'Z' (inclusive) in the collation Latin1_General_CI_AS?"

There are exactly 11 8-bit characters in the string ['0' and 'Z'] :-P

I thought this crowd liked to be literal to the point of absurdity?

Aigle de Guerre!
TomThomson
TomThomson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14256 Visits: 12197
mtassin (11/1/2012)
What's interesting for me is that the query from the original question returns 120 rows.

select CHAR(I),I from Tally
where char(I) between '0' and 'Z' collate latin1_general_ci_as
and I < 256 order by CHAR(I)



However, your query gives 139. I also realize why here (one's apply the collation to 0 thru Z the other to the result of the CHAR function. But I did find that interesting.


Actually, I suspect that the correct way to write the condition, because a collate clause applies only to a single column or constant or variable, is

where CHAR(I) COLLATE Latin1_General_CI_AS
BETWEEN '0' COLLATE Latin1_General_CI_AS
AND 'Z' COLLATE Latin1_General_CI_AS
and I < 256


That would (I hope) ensure that default collations are not used anywhere, so whatever the server default and the database default are the query will always deliver the same answer.

It was careless of me to use a query that doesn't correctly eliminate default collations in my explanation.

Tom

Ken Wymore
Ken Wymore
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5048 Visits: 2373
paul.knibbs (11/1/2012)
mtassin (11/1/2012)
Oh and of course I got it wrong... because I just went with

SELECT ASCII('Z')-ASCII('0')




That's pretty much what I did, but I did it in my head rather than using code. I was 100% sure this would be the wrong answer, mind you, I was just curious to see what the right one was! :-)


ditto!
ldemeyer@telenet.be
ldemeyer@telenet.be
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 110
I don't think the collation specification is needed after the single character constants ('0' or 'Z') in the "between"-expression, but it is after the expression 'CHAR(i)' - at least, this query gives me the right number of characters (139), although my server collation is different than latin1_general_ci_as. (it is sql_latin1_general_cp1_ci_as).

SELECT i,CHAR(i)
FROM [Test].[dbo].[Tally]
where CHAR(i) collate latin1_general_ci_as between '0' and 'Z'
and i<256
order by CHAR(i) collate latin1_general_ci_as


I added the collation specification in the order by clause: this shows the correct order : 0, 1/4, ...etc.

btw: my first answer was wrong, nice question!
ldemeyer@telenet.be
ldemeyer@telenet.be
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 110
Strange,
when i run the query that's in my previous post (with the "order by char(i) collate latin1_general_ci_as") i
have the order .... g G H h ...

Sometimes the capital is listed before and sometimes after ....

What am I missing here ?? w00t
ldemeyer@telenet.be
ldemeyer@telenet.be
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 110
Right! it's collate latin1_general_ci_as - Case INsensitive... (sorry for posting previous post too soon! )
vk-kirov
vk-kirov
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3698 Visits: 4408
L' Eomot Inversé (11/1/2012)

Actually, I suspect that the correct way to write the condition, because a collate clause applies only to a single column or constant or variable, is

where CHAR(I) COLLATE Latin1_General_CI_AS
BETWEEN '0' COLLATE Latin1_General_CI_AS
AND 'Z' COLLATE Latin1_General_CI_AS
and I < 256


That would (I hope) ensure that default collations are not used anywhere, so whatever the server default and the database default are the query will always deliver the same answer.

Incorrect. Try the following script and you'll get 62 characters:

CREATE DATABASE qod_collation_db COLLATE Japanese_CI_AS_KS_WS;
GO
USE qod_collation_db;
GO
WITH Numbers AS
( SELECT 0 AS n
UNION ALL
SELECT n + 1
FROM Numbers
WHERE n < 255
)
SELECT n AS code, CHAR(n) AS symbol
FROM Numbers AS n
WHERE CHAR(n) COLLATE Latin1_General_CI_AS BETWEEN '0' COLLATE Latin1_General_CI_AS AND 'Z' COLLATE Latin1_General_CI_AS
OPTION(MAXRECURSION 255);
GO
USE master;
GO
DROP DATABASE qod_collation_db;
GO


What's interesting if we execute 'SELECT CHAR(n)' on a Japanese_CI_AS_KS_WS database we'll get NULLs for the values of 'n' from 129 to 159 and from 224 to 252.

Another example (sorry for using a non-Latin character here; I don't know how you will see that character – in its original form, as a rectangle or something else; actually I'm using a cyrillic letter http://en.wikipedia.org/wiki/Tse_(Cyrillic)):

CREATE DATABASE qod_collation_db COLLATE Cyrillic_General_CI_AS;
GO
USE qod_collation_db;
GO
SELECT
CHAR(246) AS Cyrillic,
CHAR(246) COLLATE Latin1_General_CI_AS AS Latin,
CHAR(246) COLLATE Japanese_CI_AS_KS_WS AS Japanese;
GO
USE master;
GO
DROP DATABASE qod_collation_db;
GO


Here is the result set:

Cyrillic Latin Japanese
-------- ----- --------
ц ?

(Note that the 'Japanese' value is a whitespace.)
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27249 Visits: 13268
Great question Tom, thanks!


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Raghavendra Mudugal
Raghavendra Mudugal
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2082 Visits: 2958
Cool (actually I used extreme advanced skill to make the choice, and I still got it wrong.... but got to learn on load of things)

Awesome question. :-) Thank you Tom

ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search