Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sort Results ignoring symbols like ! ? * "

Sort Results ignoring symbols like ! ? * "

Scheduled Pinned Locked Moved Database
cssdatabasehelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AlexeiXX3
    wrote on last edited by
    #1

    Hi, thanks for looking I need to sort the results of my query, lets say i have the following values in my Articles table Id, Title -------------------------- 1, "The less you talk..." 2, The long and... 3, -ABC of Math 4, *Something else The result should look as follows: 3, -ABC of Math 4, *Something else 1, "The less you talk..." 2, The long and... Notice that the title may o may not have or may have more than 1 special character at the beggining Thanks in advance for any help

    Alexei Rodriguez

    A L 2 Replies Last reply
    0
    • A AlexeiXX3

      Hi, thanks for looking I need to sort the results of my query, lets say i have the following values in my Articles table Id, Title -------------------------- 1, "The less you talk..." 2, The long and... 3, -ABC of Math 4, *Something else The result should look as follows: 3, -ABC of Math 4, *Something else 1, "The less you talk..." 2, The long and... Notice that the title may o may not have or may have more than 1 special character at the beggining Thanks in advance for any help

      Alexei Rodriguez

      A Offline
      A Offline
      Andy_L_J
      wrote on last edited by
      #2

      Ok, I did this:

      DECLARE @Table AS Table(
      ID Int,
      [Name] nVarChar(50))

      INSERT @Table(ID, Name)
      VALUES(1, '"The less you talk..."')
      INSERT INTO @Table
      SELECT 2, 'The long and...'
      INSERT INTO @Table
      SELECT 3, '-ABC of Math'
      INSERT INTO @Table
      SELECT 4, '*Something Else'

      SELECT * FROM @Table
      ORDER BY (SELECT CASE WHEN (UNICODE(SUBSTRING(Name,1,1))< 123)
      THEN UNICODE(SUBSTRING(Name, 2,1))
      ELSE Name End) ASC

      Result:

      ID Name

      3 -ABC of Math
      4 *Something Else
      1 "The less you talk..."
      2 The long and...

      The results match what you specified, but what if there is more than one 'special' character at the beginning of the string? EDIT: If you add more data you will find the following:

      DECLARE @Table AS Table(
      ID Int,
      [Name] nVarChar(50))

      INSERT @Table(ID, Name)
      VALUES(1, '"The less you talk..."')
      INSERT INTO @Table
      SELECT 2, 'The long and...'
      INSERT INTO @Table
      SELECT 3, '-ABC of Math'
      INSERT INTO @Table
      SELECT 4, '*Something Else'
      INSERT INTO @Table
      SELECT 4, '? A Question'
      INSERT INTO @Table
      SELECT 4, '#Regions'
      INSERT INTO @Table
      SELECT 4, 'Sometimes you are right.'
      INSERT INTO @Table
      SELECT 4, 'Little creatures'

      SELECT * FROM @Table
      ORDER BY (SELECT CASE WHEN (UNICODE(SUBSTRING(Name,1,1))< 123)
      THEN UNICODE(SUBSTRING(Name, 2,1))
      ELSE Name End
      ) ASC

      Result:

      ID Name

      4 ? A Question
      3 -ABC of Math
      4 #Regions
      4 *Something Else
      1 "The less you talk..."
      2 The long and...
      4 Little creatures
      4 Sometimes you are right.

      This may not be ideal...

      I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

      modified on Saturday, April 17, 2010 12:18 AM

      A 1 Reply Last reply
      0
      • A Andy_L_J

        Ok, I did this:

        DECLARE @Table AS Table(
        ID Int,
        [Name] nVarChar(50))

        INSERT @Table(ID, Name)
        VALUES(1, '"The less you talk..."')
        INSERT INTO @Table
        SELECT 2, 'The long and...'
        INSERT INTO @Table
        SELECT 3, '-ABC of Math'
        INSERT INTO @Table
        SELECT 4, '*Something Else'

        SELECT * FROM @Table
        ORDER BY (SELECT CASE WHEN (UNICODE(SUBSTRING(Name,1,1))< 123)
        THEN UNICODE(SUBSTRING(Name, 2,1))
        ELSE Name End) ASC

        Result:

        ID Name

        3 -ABC of Math
        4 *Something Else
        1 "The less you talk..."
        2 The long and...

        The results match what you specified, but what if there is more than one 'special' character at the beginning of the string? EDIT: If you add more data you will find the following:

        DECLARE @Table AS Table(
        ID Int,
        [Name] nVarChar(50))

        INSERT @Table(ID, Name)
        VALUES(1, '"The less you talk..."')
        INSERT INTO @Table
        SELECT 2, 'The long and...'
        INSERT INTO @Table
        SELECT 3, '-ABC of Math'
        INSERT INTO @Table
        SELECT 4, '*Something Else'
        INSERT INTO @Table
        SELECT 4, '? A Question'
        INSERT INTO @Table
        SELECT 4, '#Regions'
        INSERT INTO @Table
        SELECT 4, 'Sometimes you are right.'
        INSERT INTO @Table
        SELECT 4, 'Little creatures'

        SELECT * FROM @Table
        ORDER BY (SELECT CASE WHEN (UNICODE(SUBSTRING(Name,1,1))< 123)
        THEN UNICODE(SUBSTRING(Name, 2,1))
        ELSE Name End
        ) ASC

        Result:

        ID Name

        4 ? A Question
        3 -ABC of Math
        4 #Regions
        4 *Something Else
        1 "The less you talk..."
        2 The long and...
        4 Little creatures
        4 Sometimes you are right.

        This may not be ideal...

        I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

        modified on Saturday, April 17, 2010 12:18 AM

        A Offline
        A Offline
        AlexeiXX3
        wrote on last edited by
        #3

        Thanks for the reply Its a good start, but it looks like you are only taking into account the first character and in case it is a pecial symbol you take the second, it wont work if you have values like these:

        DECLARE @Table AS Table( ID Int, [Name] nVarChar(50))
        INSERT INTO @Table SELECT 1, '"The morning..."'
        INSERT INTO @Table SELECT 2, 'The long and...'
        INSERT INTO @Table SELECT 3, '-ABC of Math'
        INSERT INTO @Table SELECT 4, '*Something Else'

        SELECT * FROM @Table
        ORDER BY (SELECT CASE WHEN (UNICODE(SUBSTRING(Name,1,1))< 123)
        THEN UNICODE(SUBSTRING(Name, 2,1))
        ELSE Name End) ASC

        Notice how "The morning..." appears before The Long and... :( Thanks again for your example

        Alexei Rodriguez

        A 1 Reply Last reply
        0
        • A AlexeiXX3

          Thanks for the reply Its a good start, but it looks like you are only taking into account the first character and in case it is a pecial symbol you take the second, it wont work if you have values like these:

          DECLARE @Table AS Table( ID Int, [Name] nVarChar(50))
          INSERT INTO @Table SELECT 1, '"The morning..."'
          INSERT INTO @Table SELECT 2, 'The long and...'
          INSERT INTO @Table SELECT 3, '-ABC of Math'
          INSERT INTO @Table SELECT 4, '*Something Else'

          SELECT * FROM @Table
          ORDER BY (SELECT CASE WHEN (UNICODE(SUBSTRING(Name,1,1))< 123)
          THEN UNICODE(SUBSTRING(Name, 2,1))
          ELSE Name End) ASC

          Notice how "The morning..." appears before The Long and... :( Thanks again for your example

          Alexei Rodriguez

          A Offline
          A Offline
          Andy_L_J
          wrote on last edited by
          #4

          Yes, I have tried quite a few things. You will have to parse the data in the client and perform your sorting there. This is the correct way. The database is for data and not particularly interested in the formatting.

          I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

          1 Reply Last reply
          0
          • A AlexeiXX3

            Hi, thanks for looking I need to sort the results of my query, lets say i have the following values in my Articles table Id, Title -------------------------- 1, "The less you talk..." 2, The long and... 3, -ABC of Math 4, *Something else The result should look as follows: 3, -ABC of Math 4, *Something else 1, "The less you talk..." 2, The long and... Notice that the title may o may not have or may have more than 1 special character at the beggining Thanks in advance for any help

            Alexei Rodriguez

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            AlexeiXX3 wrote:

            Notice that the title may o may not have or may have more than 1 special character at the beggining

            The best I can think of is a UDF that uses the REPLACE function to strip those characters. You could add this on a separate view on the table, including the column only when needed- as it would be a very expensive operation (looping all characters for each record). It might be better (performance-wise) to store a redundant version of the Title, without any special characters.

            I are Troll :suss:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups