Solved

sort a sql text field by date

Posted on 2007-03-28
6
487 Views
Last Modified: 2010-07-27
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
20070225-20070303.txt                            

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
RUNDATE = Rs("RUNDATE")
rundate2=LEFT(rundate,8)
rundate2=FormatDateTime(rundate2,2)
'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>")

    Rs.MoveNext
loop
end if

Rs.Close
set Rs = Nothing

conn.close
set conn= nothing
%>
0
Comment
Question by:cseink
6 Comments
 
LVL 5

Accepted Solution

by:
jalalmegadeth earned 65 total points
ID: 18808633
hi
you can try
strSQL2 = "Select distinct RUNDATE from edstats order by RUNDATE"
or
strSQL2 = "Select distinct RUNDATE from edstats order by RUNDATE desc"


GL
0
 

Author Comment

by:cseink
ID: 18808679
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?
0
 
LVL 2

Expert Comment

by:MeDude21
ID: 18808767
No. The order will be fine. As long as RUNDATE is a datetime field it will store it in the correct format.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:cseink
ID: 18811001
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.??
0
 
LVL 16

Assisted Solution

by:ThinkPaper
ThinkPaper earned 65 total points
ID: 18811288
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:
http://msdn2.microsoft.com/en-us/library/aa258891(SQL.80).aspx

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
0
 

Author Comment

by:cseink
ID: 18811312
Great Thanks for the info
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP Classic - Load test 2 55
UTC (timezone) without using an API 16 50
ASP CDO - Limiting Connections 3 59
Connecting to multiple databases to create a Dashboard 5 48
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question