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

Sorting issue when meet there is ASCII Expand / Collapse
Author
Message
Posted Thursday, July 25, 2013 2:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 01, 2013 1:45 AM
Points: 29, Visits: 150
Hi there, I am facing a sorting issue explained as below and looking for help please,

Result from first query,
Select part_code from s_stkmst
WHERE part_code>='RKS0516'
ORDER BY part_code collate SQL_Latin1_General_Cp1251_CS_AS

RKS0516-W
RKS0516-W-Z
RKS0516A-W
RKS0516A-W-Z
RKS0530-A
RKS0530-A-SP
RKS0530-A-Z
RKS0530-A1


Result from second query,
Select part_code from s_stkmst
WHERE part_code>='RKS0516-W'
ORDER BY part_code collate SQL_Latin1_General_Cp1251_CS_AS

RKS0516-W
RKS0516-W-Z
RKS0530-A
RKS0530-A-SP
RKS0530-A-Z
RKS0530-A1


The second result from the query is missing 2 records 'RKS0516A-W' and 'RKS0516A-W-Z'. I had tries other collate and couldn't get it right.


Together, we can make wonders!
Post #1477380
Posted Thursday, July 25, 2013 2:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:58 AM
Points: 5,077, Visits: 8,918
That'll be because "A" comes after "-" in SQL_Latin1_General_Cp1251_CS_AS and all the other collations you've tried. Here's a few options I can think of:

(1) Find a collation that has "-" after all the alphanumeric characters
(2) Change your database design so that you store the separate parts of your codes in separate columns
(3) Use a string splitter (search this site to find one) to split your codes into their separate parts before you do your ORDER BY
(4) Use REPLACE to replace "-" with some other character that comes after all alphanumeric characters in the collation and that does not appear in any of the codes. Replace that character with "-" again once you've done your sorting.
(5) Use a more generalised form of the query to make sure you return all the data you need and then have your presentation layer do the customised sorting and filtering

John

Edit: added option 5
Post #1477390
Posted Thursday, July 25, 2013 2:41 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 01, 2013 1:45 AM
Points: 29, Visits: 150
John Mitchell-245523 (7/25/2013)
That'll be because "A" comes after "-" in SQL_Latin1_General_Cp1251_CS_AS and all the other collations you've tried. Here's a few options I can think of:

(1) Find a collation that has "-" after all the alphanumeric characters
(2) Change your database design so that you store the separate parts of your codes in separate columns
(3) Use a string splitter (search this site to find one) to split your codes into their separate parts before you do your ORDER BY
(4) Use REPLACE to replace "-" with some other character that comes after all alphanumeric characters in the collation and that does not appear in any of the codes. Replace that character with "-" again once you've done your sorting.
(5) Use a more generalised form of the query to make sure you return all the data you need and then have your presentation layer do the customised sorting and filtering

John

Edit: added option 5


Thank you so much for your fast reply.

Certainly, option 2 is not possible because it may affect system design. I had found a similar example in this forum as your option 4 as below but result is the same,

SELECT part_code,
REPLACE(REPLACE(part_code,'-',' Z'),' ',' ')
FROM s_stkmst
WHERE part_code >= 'RKS0516-W'
ORDER BY REPLACE(REPLACE(part_code,'-',' Z'),' ',' ') collate SQL_Latin1_General_Cp1251_CS_AS




Together, we can make wonders!
Post #1477397
Posted Thursday, July 25, 2013 3:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:58 AM
Points: 5,077, Visits: 8,918
I think this is because you're putting a space before your "Z". And the outer REPLACE doesn't do anything - it just replaces a space with a space.

John
Post #1477408
Posted Thursday, July 25, 2013 8:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 01, 2013 1:45 AM
Points: 29, Visits: 150
John Mitchell-245523 (7/25/2013)
I think this is because you're putting a space before your "Z". And the outer REPLACE doesn't do anything - it just replaces a space with a space.

John


I have tried and playing around the REPLACE but no luck. Can you please show an example of how to resolve this scenario? TQ


Together, we can make wonders!
Post #1477543
Posted Thursday, July 25, 2013 8:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:58 AM
Points: 5,077, Visits: 8,918
If removing the space before the Z doesn't do it, please post table DDL in the form of CREATE TABLE statements, sample data in the form of INSERT statements, and the results you expect to see given the sample data.

John
Post #1477544
Posted Thursday, July 25, 2013 9:21 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 01, 2013 1:45 AM
Points: 29, Visits: 150
John Mitchell-245523 (7/25/2013)
If removing the space before the Z doesn't do it, please post table DDL in the form of CREATE TABLE statements, sample data in the form of INSERT statements, and the results you expect to see given the sample data.

John



Hi John,
Thanks again.
I reproduce the scenario from same SQL 2008 R2 server and here below is as per your request,

CREATE TABLE [dbo].[A_Test] (
[part_code] varchar(50) COLLATE Chinese_PRC_Stroke_90_CI_AS NOT NULL
)
ON [PRIMARY]
GO

INSERT INTO [dbo].[A_Test] ([part_code])
VALUES (N'RKS0516-W')
GO

INSERT INTO [dbo].[A_Test] ([part_code])
VALUES (N'RKS0516-W-Z')
GO

INSERT INTO [dbo].[A_Test] ([part_code])
VALUES (N'RKS0516A-W')
GO

INSERT INTO [dbo].[A_Test] ([part_code])
VALUES (N'RKS0516A-W-Z')
GO

INSERT INTO [dbo].[A_Test] ([part_code])
VALUES (N'RKS0530-A')
GO

INSERT INTO [dbo].[A_Test] ([part_code])
VALUES (N'RKS0530-A-SP')
GO

1) Result from this query is correct,
----------------------------------
Select part_code,
from A_Test
WHERE part_code>='RKS0516'
ORDER BY part_code collate SQL_Latin1_General_Cp437_BIN

RKS0516-W
RKS0516-W-Z
RKS0516A-W
RKS0516A-W-Z
RKS0530-A
RKS0530-A-SP

But, when I change the filter value from 'RKS0516' to 'RKS0516-W', the result shows missing of 2 records
RKS0516-W
RKS0516-W-Z
RKS0530-A
RKS0530-A-SP

I am expecting the result should like the first query.



Together, we can make wonders!
Post #1477568
Posted Friday, July 26, 2013 4:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:58 AM
Points: 5,077, Visits: 8,918
OK, you seem to have changed your collation, so here's what to do in general:

(1) Go on to Wikipedia and search for "code page xxxx" where xxxx is the three or four digits that follow "Cp" in the name of the collation you are using. You'll come to a page like this.
(2) Find a character in the character set that comes after every character that is used in your part_code.
(3) Replace "-" in your part_codes with that character. Do that with a REPLACE function similar to the inner REPLACE you used before, but with the new character instead of Z and without a space before it. Put the REPLACE on both sides of the ">=". You don't need the outer REPLACE that you tried in your previous example.
(4) You should end up with something like this:
Select part_code
from A_Test
WHERE REPLACE(part_code,'-','~') COLLATE SQL_Latin1_General_Cp437_BIN >= REPLACE('RKS0516','-','~') COLLATE SQL_Latin1_General_Cp437_BIN
ORDER BY part_code collate SQL_Latin1_General_Cp437_BIN

John
Post #1477936
Posted Friday, July 26, 2013 8:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 01, 2013 1:45 AM
Points: 29, Visits: 150
John, sorry for the confusion. I was trying many ways and mixed up the original code page I posted initially. Thanks for your tips and I will try it.

Together, we can make wonders!
Post #1478032
Posted Friday, July 26, 2013 8:59 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, November 01, 2013 1:45 AM
Points: 29, Visits: 150
Seems like this works for me so far,

Select part_code, REPLACE(part_code,'-','#')
from A_Test
WHERE REPLACE(part_code,'-','#') COLLATE SQL_Latin1_General_Cp437_BIN >= REPLACE('RKS0516-W-Z','-','#') COLLATE SQL_Latin1_General_Cp437_BIN
ORDER BY REPLACE(part_code,'-','#') collate SQL_Latin1_General_Cp437_BIN

Thanks John


Together, we can make wonders!
Post #1478058
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse