Click here to Skip to main content
15,894,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Would anyone please help me. I trying to sort out (natural order) in a Firebird 2.1 database - a column named 'PARTICULARS' - varchar(35) that has record like this:

'1 of 20% item'
'10 of 20% item'
'2 of 20% item'
'5 of 20% item'
.
.
.


to:

'1 of 20% item'
'2 of 20% item'
'5 of 20% item'
'10 of 20% item'


I have done numerous sql command but with no luck:

SQL
select * from UNPAID_FORM order by cast(Case patIndex('', LTrim (PARTICULARS)) when 0 then LTrim (PARTICULARS) else substring (LTrim (PARTICULARS), 1, patIndex ('', LTrim (PARTICULARS)) - 1)End as int) asc

select * from UNPAID_FORM order by substring(PARTICULARS from 1 for patindex ('', LTrim (PARTICULARS)-1)

select PARTICULARS from UNPAID_FORM order by 1 nulls last


note: UNPAID_FORM is my table - thanks in advance.

makoy
Posted
Updated 14-Apr-13 21:33pm
v2

1 solution

Try this:
SQL
SELECT *
FROM (
    SELECT CONVERT(INT, LEFT(PARTICULARS, CHARINDEX('of',PARTICULARS)-1)) AS ID, PARTICULARS
    FROM UNPAID_FORM
    ) AS T
ORDER BY ID


Result:
1   1 of 20% item
2   2 of 20% item
5   5 of 20% item
10  10 of 20% item


[EDIT #1]
As mentioned, it's Firebird database engine.
SQL
SELECT * FROM (
    SELECT CAST(LEFT(PARTICULARS, POSITION(' of' IN PARTICULARS)) AS INT) AS ID, PARTICULARS
    FROM UNPAID_FORM
    ) AS T
ORDER BY ID


Many interesting functions for Firebird, you'll find here: http://www.volny.cz/iprenosil/interbase/ip_ib_code_string.htm[^]
 
Share this answer
 
v2
Comments
makoy03 15-Apr-13 3:45am    
@ Maciej Los: thanks i'll try it now
makoy03 15-Apr-13 4:40am    
hello Maciej: I did something like this:

SELECT * FROM (SELECT CAST(LEFT(PARTICULARS, POSITION('of' IN PARTICULARS)-1)AS INT) AS ID, PARTICULARS FROM UNPAID_FORM) AS T ORDER BY ID

and arrived in this kind of error : "Invalid length parameter -1 SubString. Negative INT are not allowed." But when I omit '-1'

SELECT * FROM (SELECT CAST(LEFT(PARTICULARS, POSITION('of' IN PARTICULARS)AS INT) AS ID, PARTICULARS FROM UNPAID_FORM) AS T ORDER BY ID

It returned this error: "Conversion error from string '1 o'". what should i do??
Maciej Los 15-Apr-13 4:51am    
Position(... IN ...) - is it Firebird?
makoy03 15-Apr-13 4:52am    
yes. it is.
Maciej Los 15-Apr-13 5:01am    
You need to use POSITION[^] with SUBSTRING[^] or LEFT[^] function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900