• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Process the result so that the dates are ordered by date

Enthusiast ,
Sep 18, 2008 Sep 18, 2008

Copy link to clipboard

Copied

I am retrieving a query result from a sql database of which one column contains a string of this format:

09/08/2008

How can I process this result so that the dates are ordered by date?

Thanks for anu help 🙂
TOPICS
Advanced techniques

Views

530

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 19, 2008 Sep 19, 2008
what's your db and version?
pretty much all dbs have built-in string and date functions, a
combinations of which will let you update your field to be date/datetime.

you will probably want to create a new field in the table to store the
converted dates, then check that they all have been converted correctly,
then delete the current date field and rename the new field to the old
date field's name.

using your db's string functions you will need to extract day, month and
year parts of your date te...

Votes

Translate

Translate
LEGEND ,
Sep 18, 2008 Sep 18, 2008

Copy link to clipboard

Copied

store your dates as dates, not as text.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

quote:

Originally posted by: Newsgroup User
store your dates as dates, not as text.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/



What advantage is there in this, the string seems to work fine?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

quote:

Originally posted by: nikos101

What advantage is there in this, the string seems to work fine?

You wouldn't have trouble sorting if you were storing dates as dates.
If you ever need to do date based queries, such as what happened last month, you can't.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

work fine? and that's why you were asking your original question, right?

dates are dates - date objects - that your db knows how to properly
handle when sorting, grouping, extracting a particular date part, etc etc.

strings are just text - your db has no idea what actual date it
represents. as you have found out, it won't even order your query
results as it would if your dates were dates.

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 18, 2008 Sep 18, 2008

Copy link to clipboard

Copied

Go through the cast functions of your SQL documentation and try to cast your string field as date after the order by clause.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 18, 2008 Sep 18, 2008

Copy link to clipboard

Copied

CASTing the column to a datetime value should work. However, Azadi is correct. Dates should be stored as dates, not text.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 18, 2008 Sep 18, 2008

Copy link to clipboard

Copied

Set the data type to datetime and just order by as normal.

<cfquery name="query_name" datasource="data_source">
select …, …, …, date_string from table_name
order by date_string
</cfquery>
<cfoutput query=" query_name ">
#lsDateFormat(date_string, 'dd/mm/yyyy')#<br />
</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

I think I will convert it to datetime. How can I do this since I have many rows that contain the stringdates?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

what's your db and version?
pretty much all dbs have built-in string and date functions, a
combinations of which will let you update your field to be date/datetime.

you will probably want to create a new field in the table to store the
converted dates, then check that they all have been converted correctly,
then delete the current date field and rename the new field to the old
date field's name.

using your db's string functions you will need to extract day, month and
year parts of your date text, and pass them to your db's date creation
function.

if your db is MySQL, it has a very handy STR_TO_DATE(str, format)
function... check details in mysql ref manual.



Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

Thanks Azadi,

I created a datetime column called dateAdded2 and run the following query:

UPDATE staff
SET [dateAdded2] = dateAdded

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

LATEST
nikos101 wrote:
> I created a datetime column called dateAdded2 and run the following query:
> UPDATE staff
> SET [dateAdded2] = dateAdded

Unless you understand how your database handles string -> date conversions, it is better to use the approach Azadi mentioned. Use a function that tells the database exactly how it should interpret the string. For MS SQL use the convert function:

convert(datetime, yourColumn, format)

Some date strings are ambiguous. So if you do not tell the database the format of the value it is converting, it may interpret the date correctly .. or it may not.

http://msdn.microsoft.com/en-us/library/ms187928.aspx

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 19, 2008 Sep 19, 2008

Copy link to clipboard

Copied

Yes sorting datetime columns is now a dream for me!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation