Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Sort By Date??

Sort By Date??

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tadhg88
    wrote on last edited by
    #1

    Hi i am using this sql statement on (SQL Server 2000) and tryin to sort the results on tl.Date but as i have already casted it to a nvarchar it sorts it like a char so im just wondering if anyone has any idea how to sort it by date correctly. thanks in advance Tim select SUBSTRING(CAST(tl.Date as nvarchar), 0, 12) as 'Date', tft.BodyWeight from Person as p left join TrainingLog tl on tl.PersonId = p.Id left join TrainingFitnessTesting tft on tl.Id = tft.TrainingLogId where p.Id = 8 and tl.Date >= '01 January 2005' and tl.Date <= '03 January 2006' and tft.BodyWeight IS NOT NULL order by tl.Date asc

    S 1 Reply Last reply
    0
    • T tadhg88

      Hi i am using this sql statement on (SQL Server 2000) and tryin to sort the results on tl.Date but as i have already casted it to a nvarchar it sorts it like a char so im just wondering if anyone has any idea how to sort it by date correctly. thanks in advance Tim select SUBSTRING(CAST(tl.Date as nvarchar), 0, 12) as 'Date', tft.BodyWeight from Person as p left join TrainingLog tl on tl.PersonId = p.Id left join TrainingFitnessTesting tft on tl.Id = tft.TrainingLogId where p.Id = 8 and tl.Date >= '01 January 2005' and tl.Date <= '03 January 2006' and tft.BodyWeight IS NOT NULL order by tl.Date asc

      S Offline
      S Offline
      S Akif Kamal
      wrote on last edited by
      #2

      As per my knowledge this query will work fine and will display the result sorted based on tl.Date as Date. I may be mis-understanding your question but I think as you have converted Date into nvarchar in select clause not in Order By clause, hence it will have no effect on the sorting. Akif

      T 1 Reply Last reply
      0
      • S S Akif Kamal

        As per my knowledge this query will work fine and will display the result sorted based on tl.Date as Date. I may be mis-understanding your question but I think as you have converted Date into nvarchar in select clause not in Order By clause, hence it will have no effect on the sorting. Akif

        T Offline
        T Offline
        tadhg88
        wrote on last edited by
        #3

        someone has actually solved the problem now but just in case u would like to know when i got a substring of the date i got dates like Dec 12 2005 Sept 23 2005 sorted incorrectly as sept should be before dec but because they are nvarchar they are sorted as characters as in D comes before S in the alphabet hopes this clears it up thanks anyway Tim

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups