sort a sql text field by date

I have a field in an sql database that I want to sort in a list. It's a text value that stores the file name where the data was imported from by its week range

How can I sort this field by date in the sql statement.

' here is my start

strSQL2 = "Select distinct RUNDATE from edstats"

Rs.Open strSQL2, Conn
if   not Rs.EOF then
do until rs.EOF
'this is what I want to display by date order soonest to oldest
RESPONSE.WRITE("<A href='LOADEXCEL.ASP?value="&rundate&"'>Week of "&rundate2&"<BR>")

end if

set Rs = Nothing

set conn= nothing
Who is Participating?
jalalmegadethConnect With a Mentor Commented:
you can try
strSQL2 = "Select distinct RUNDATE from edstats order by RUNDATE"
strSQL2 = "Select distinct RUNDATE from edstats order by RUNDATE desc"

cseinkAuthor Commented:
OK. That is working for now but I only have 3 weeks in there. Will it get out of order if the field is not formatted somehow?
No. The order will be fine. As long as RUNDATE is a datetime field it will store it in the correct format.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cseinkAuthor Commented:
The field is text field not a datetime. Is there any way through sql to convert  the field to a datetime in 20070311-20070317.txt  is how the field is stored in the DB  depending on the week.  Read the first 8 chars or something.??
ThinkPaperConnect With a Mentor IT ConsultantCommented:
so your "date" field looks like: "20070311-20070317.txt "  ? If the filename somehow does get out of format, it will throw your order off. But let's hope you've provided all the checks & limitations so it won't be off.

You can also consider using the string functions provided in SQL to format it, but it would be basically the same method you'd be doing now. You'd still be grabbing the left side for the date. And again, if the name is messed up, the query would mess up:

not sure if this is completely right, but you get the idea:
Select distinct RUNDATE, LEFT(RUNDATE,4) as runYear from edstats ORDER BY runYear
cseinkAuthor Commented:
Great Thanks for the info
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.