Solved

sort a sql text field by date

Posted on 2007-03-28
6
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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