Adding days to a date then sorting by the new date

Posted on 2005-04-19
Last Modified: 2010-03-19
Hi All,

I'm having some trouble figuring out how to sort by a date which is not stored in the database but which is calculated on-the-fly on my webpage by adding a number of days to another date.

So we have the following fields in the table UserDetails:

Date (smalldatetime ('dd/mmyyyy')
Term (int)

I am trying to sort my results by DueDate (which is dynamically calculated and not stored as a field in the db) which is Date ('dd/mm/yyyy') + Term (number of days (int) ).

So I am wondering if there is anyway that inside my SELECT in my SQL statement I can actually create a new date AS DueDate which is the above sum?

Anyone have any ideas?

Question by:bradderick
    LVL 15

    Accepted Solution

    Hi there

    Afraid I dont have SQL here at home so I cant test it first before suggesting it but I believe it should work

    SELECT [Date] + Term AS DueDate
    FROM UserDetails
    ORDER BY  [Date] + Term

    This should return the newly created DueDate field with the results sorted on DueDate

    I cant remember if Date is a reserved word or not that is why it has square brackets around it.



    Author Comment

    Thanks Scott! That worked really well and the ORDER BY worked using the alias DueDate as well.

    It seems so simple :o) I really appreciate your help.

    LVL 15

    Expert Comment

    Your welcome, glad it worked for you and thanks for the grade



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This is my first ever Article on EE or elsewhere; therefore, please bear with me if I have some discrepancies in my writing. I read many articles and questions related to "how to pass values to SSIS packages at run-time?"  Hence, this common ques…
    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    734 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

    24 Experts available now in Live!

    Get 1:1 Help Now