DBAsupport.com Forums - Powered by vBulletin
Results 1 to 7 of 7

Thread: order a query by days of week starting with monday

  1. #1
    Join Date
    Jan 2010
    Posts
    3

    order a query by days of week starting with monday

    hello. suppose a table XYZ has a column buydate. and there are 14 rows in it, 2 for each day of the week. if i would like to sort a query by days of the week, but starting from monday, how would i be able to do that? i have tried this query :
    select * from XYZ order by to_char(buydate,'D') ;

    this would bring sunday as the first row.
    i dont want sunday as the first row, i want monday.

    i am using oracle 10g database

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    / / / decode(to_char(buydate,'D') ,1,7,2,1,3,2,4,3,5,4,6,5,7,6,to_char(buydate,'D'))
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Apr 2006
    Posts
    377
    Another option:

    Code:
    order by decode(to_char(buydate,'D'), 1, 8, to_char(buydate,'D'))

  4. #4
    Join Date
    Jan 2010
    Posts
    3
    thanks...but would you please also explain why you chose that approach? thanks

  5. #5
    Join Date
    Jan 2010
    Posts
    3
    could you explain why you chose this approach?

  6. #6
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by umair123 View Post
    could you explain why you chose this approach?
    mmhh... may be because it solves the issue?
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  7. #7
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    You can also subtract one day from the date field where Sunday becomes Saturday and Monday becomes Sunday hence it would sort the days from Monday to Sunday.

    Code:
    select * from XYZ order by to_char(buydate-1,'D') ;

Tags for this Thread

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