Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I vant to SORT a recordset Query 1

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
0
0
US
In my ASP page I have a SQL query that gives me a single field from a table. But the recordset is populated using the 1, 10, 11, 2 method... whatever you call it. I want it to go 1,2,3...10,11,

How can I do that?
 
Simply change your SQL
to:

[tt]SQL="SELECT * FROM Your Table ORDER BY Field to sort ASC"[/tt]


[tt]ASC = Ascending order
Des = Descending order
[/tt]
Hope it helps...
 
K, cept Des give me this error:
Error Type:
Microsoft OLE DB Provider for SQL Server (0x80040E14)
Line 1: Incorrect syntax near 'DES'.
/includeSets.asp, line 143

And Asc still gives me this:
1
10
11
13
15
16
17
18
2
3
4
5
6
7
8
9
 
the statement should end like this:

ORDER BY Your field desc"
and not ORDER BY Your field 'desc'

My bad, had a typo.
 
If you're still getting the records sorted like "1, 10, 11, 12" then you're probably not using a number data type for that field - more likely it's a string type. Check your data type.
 
What if I cannot change the datatype? what if there is another application using this database?
 
Depends on the database you are using... their maybe some functions you can use which are part of your databases unique api. Brett Birkett B.Comp
Systems Analyst
 
Try converting the value to a number before the order by :

SQL ="SELECT * FROM Your Table ORDER BY to_number(Field to Sort) ASC"


( This may not be SqlServer ( it works in Oracle) but you should get the idea..)
SQL> select * from temp_testx order by f1 asc;

F1
--
1
10
2
20

1* select * from temp_testx order by to_number(f1) asc
SQL> /

F1
--
1
2
10
20
 
Has anybody used the Convert funtion in a SQL query? Ex. from 'SQL Server 2000 Programming By Example' (By Que Publishing):

SELECT orderid, customerid, employeeid, orderdate FROM Orders WHERE CONVERT(VARCHAR(20),orderdate,102) = '1996.07.04'

I would like more information on how that works. (Pouring thru the book as we speak.)
 
Okay, I got it. Here's the final Search string. It takes the data which is saved as char and converts it to int, then of course sorts ascending. I'm so proud of myself.

SELECT Distinct ID = CONVERT(int, ID) FROM CustomerProducts WHERE CustomerID = '&quot; & custid & &quot;' AND ProgramID = '&quot; & searchID & &quot;' AND ID <> 'SLT' Order BY ID Asc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top