tags:

views:

214

answers:

5
A: 
SELECT *
 FROM table
ORDER BY CASE WHEN len(col) >= 2
              THEN REPLACE(LEFT(COL, 1), '"', '') 
                 + RIGHT(LEFT(col, LEN(col)-1), LEN(LEFT(col, LEN(col)-1))-1) 
                 + REPLACE(RIGHT(col, 1), '"', '')
              ELSE col
         END

in the ORDER BY, I eliminate double quotes by replacing it 'within' 1st & last characters by ''. Of course it's only valid when col length >= 2.

najmeddine
+2  A: 

Depends on what language you are using. In MS SQL, I use the REPLACE command. So if I have a table, users, with a column first name that I am sorting by, I do this which strips all the double quotes out of the string:

SELECT     *
FROM         dbo.Users
ORDER BY REPLACE(firstName, '"', '')
JohnathanKong
REPLACE will remove all the quotes from the name.
Lukasz Lysik
You're right. But it only removes it when sorting, not from the data itself. It seems that kylex just wants to sort everything without quotes. so this will sort things like:Ar"ticle 3Bad example, but that's all I can think of right now. Either way, since it is not affecting the data returned, I don't think there will be a problem.
JohnathanKong
But doesn't it affect the sort order in some way?
Lukasz Lysik
It does affect the sort order, but only for items that have double quotes in them ("). So any names that don't have double quotes will be ordered normally, and any names that have quotes in them will be stripped and ordered accordingly. So I guess it would be like: Normal Ordered Names: Article "2, Article 1 Stripped Quote Order Article 1, Article "2. So basically it modifies the order for what he needs, which is ordering without quotes. If he wanted to just ignore the quote if it was at the start, then this would not work. Sorry about the mess. It's hard for me to type without formatting opts
JohnathanKong
+2  A: 
SELECT * FROM yourTable ORDER BY TRIM(BOTH '"' FROM title);
Lukasz Lysik
Worked beautifully!
kylex
A: 
select name  from myTable order by REPLACE(name, '"', '')
mr_dunski
+1  A: 

A trick that may be acceptable, and faster than TRIM() or REPLACE() based solution is to focus only on the first character being a quote (or by extension a non alphanumeric character).

ORDER BY CASE LEFT(myCol, 1) 
      WHEN '"' THEN REPLACE(myCol, '"', '')
      ELSE myCol
    END CASE

In general, for bigger dataset, this kind of processing should be done at load time, for example with the addition of columns for querying purposes as opposed to columns for display purposes.

mjv