Solved

sort a sql text field by date

Posted on 2007-03-28
6
482 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now