Solved

sort a sql text field by date

Posted on 2007-03-28
6
479 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

11 Experts available now in Live!

Get 1:1 Help Now