Results 1 to 9 of 9

Thread: SQL Server 2000 Order By Date

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    SQL Server 2000 Order By Date

    Hi,

    I'm using SQL Server 2000 and am having a bit of trouble sorting by date

    I've been getting incorrect results till I started converting the date to

    YYYYMMDD - ORDER BY CONVERT(CHAR(8),a.Date_Assigned,112) DESC

    The above is great if multiple records aren't inserted a day.

    Does anyone know the code to convert a date to YYYYMMDDHHMMSS?

    Cheers Al

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2000 Order By Date

    If you are using DATETIME datatype you should not have a problem with ORDER BY DATEASSIGNED

    I believe I have seen cases where the ORDER by used the select-list column for ordering - maybe that was a situation where a UNION was used to put two SELECT statements together.

    From BOL - other CONVERT styles are:

    Without century (yy) With century (yyyy)
    Standard
    Input/Output**
    - 20 or 120 (*) ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
    - 21 or 121 (*) ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)
    - 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(no spaces)
    I would really like to see more of the query you are using - I do not believe that CONVERT() is required.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: SQL Server 2000 Order By Date

    Hi,

    Think I might have a solution

    Code:
    CONVERT(VARCHAR(23),GETDATE(),126) 
    
    --Outputs YYYY-MON-DDTHH:MM:SS.MMM(24H) FORMAT
    Cheers Al

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2000 Order By Date

    I would still like to see your whole query...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: SQL Server 2000 Order By Date

    szlamany,

    Sorry just missed your post

    I'm using datetime datatype, when ordering by date found that 2007 dates were coming out in the wrong order

    Here is my query which was getting odd results

    Code:
    SELECT l.Leave_Id, CONVERT(CHAR(11),l.Start_Date,106) As Start_Date, CONVERT(CHAR(11),l.End_Date,106) As End_Date, l.Status, l.Leave_Type, l.Revised_Papers_Chk, l.Comment,
    	 CASE WHEN CONVERT(CHAR(8),l.Start_Date,112) <= CONVERT(CHAR(8),GETDATE()+@Lead,112) AND CONVERT(CHAR(8),l.End_Date,112) >= CONVERT(CHAR(8),GETDATE(),112) THEN 'red' -- Currently on Leave
    	 WHEN CONVERT(CHAR(8),l.Start_Date,112) > CONVERT(CHAR(8),GETDATE(),112) THEN 'blue' -- Future
    	 WHEN CONVERT(CHAR(8),l.End_Date,112) < CONVERT(CHAR(8),GETDATE(),112) THEN 'black' -- History/In the past
    	 ELSE 'green' END "Colour_Code" -- UnKnown 
    FROM Leave l
    WHERE l.User_ID=@User_ID
    ORDER BY CONVERT(VARCHAR(23),l.Start_Date,126) Desc

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2000 Order By Date

    Just to diagnose this a bit - could you try this?

    Code:
    SELECT l.Leave_Id, CONVERT(CHAR(11),l.Start_Date,106) As Start_Date_Col, CONVERT(CHAR(11),l.End_Date,106) As End_Date, l.Status, l.Leave_Type, l.Revised_Papers_Chk, l.Comment,
    	 CASE WHEN CONVERT(CHAR(8),l.Start_Date,112) <= CONVERT(CHAR(8),GETDATE()+@Lead,112) AND CONVERT(CHAR(8),l.End_Date,112) >= CONVERT(CHAR(8),GETDATE(),112) THEN 'red' -- Currently on Leave
    	 WHEN CONVERT(CHAR(8),l.Start_Date,112) > CONVERT(CHAR(8),GETDATE(),112) THEN 'blue' -- Future
    	 WHEN CONVERT(CHAR(8),l.End_Date,112) < CONVERT(CHAR(8),GETDATE(),112) THEN 'black' -- History/In the past
    	 ELSE 'green' END "Colour_Code" -- UnKnown 
    FROM Leave l
    WHERE l.User_ID=@User_ID
    ORDER BY l.Start_Date Desc

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: SQL Server 2000 Order By Date

    Your query worked fine, when I changed the column alias back to what I originally had

    As Start_Date_Col --> As Start_Date

    I got the wrong order again

    The thing is I would like to keep the original column name

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server 2000 Order By Date

    From what GOOGLE appears to be telling me you have stumbled upon the fact that the ORDER by is using the column in the SELECT list (this is a requirement in the SQL-92 standard - but not in SQL-99 - maybe it's a compatibility setting on your server).

    http://www.thescripts.com/forum/thread79214.html gives a hint of what I believe this is related to.

    I've seen this behavior myself - basically you CONVERT or CAST a column in the SELECT list and all of a sudden the ORDER BY starts using that re-constituted data for sorting. I have also done what you are doing - CONVERT or CAST again in the ORDER BY to get around this.

    Thanks for taking the time to show that the ORDER BY was in fact using the "select-list" column. Maybe someone will come along with more info for us...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: SQL Server 2000 Order By Date

    No, no it is I who should be thanking you, I managed to to get it working without understanding what the problem was, treating the symptoms and not the cause if you like.

    Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width