Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


character ordering


character ordering

Author
Message
paul.knibbs
paul.knibbs
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1992 Visits: 6214
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
SSChasing Mays
SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)SSChasing Mays (641 reputation)

Group: General Forum Members
Points: 641 Visits: 761
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 (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10787 Visits: 12022
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
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4456 Visits: 2343
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
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 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
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 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
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 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.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3494 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
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16548 Visits: 13210
Great question Tom, thanks!



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

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Raghavendra Mudugal
Raghavendra Mudugal
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1702 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