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 «««1234»»

character ordering Expand / Collapse
Author
Message
Posted Thursday, November 01, 2012 8:41 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:53 AM
Points: 1,528, Visits: 5,171
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!
Post #1379858
Posted Thursday, November 01, 2012 10:13 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, February 16, 2014 3:14 PM
Points: 471, Visits: 509
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']

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


Aigle de Guerre!
Post #1379920
Posted Thursday, November 01, 2012 12:09 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 4:56 PM
Points: 8,271, Visits: 8,717
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
Post #1379963
Posted Thursday, November 01, 2012 12:54 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 9:46 AM
Points: 3,150, Visits: 1,900
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!
Post #1379981
Posted Thursday, November 01, 2012 5:42 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 29, 2014 5:01 AM
Points: 66, Visits: 105
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!
Post #1380092
Posted Thursday, November 01, 2012 5:55 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 29, 2014 5:01 AM
Points: 66, Visits: 105
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 ??
Post #1380093
Posted Thursday, November 01, 2012 6:01 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, January 29, 2014 5:01 AM
Points: 66, Visits: 105
Right! it's collate latin1_general_ci_as - Case INsensitive... (sorry for posting previous post too soon! )
Post #1380094
Posted Friday, November 02, 2012 3:05 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, April 10, 2014 7:08 AM
Points: 3,448, Visits: 4,406
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.)
Post #1380211
Posted Friday, November 02, 2012 4:36 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:27 PM
Points: 12,185, Visits: 9,134
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 LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1380262
Posted Friday, November 02, 2012 7:59 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 8:09 AM
Points: 669, Visits: 1,538
(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
--
There are only 10 types of people in the world, those who understand binary, and those who don't.

Note: (as of now) only.. 1 and 4 applies (i am on my way...)
Post #1380387
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse