-
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
-
/ / / 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.
-
Another option:
Code:
order by decode(to_char(buydate,'D'), 1, 8, to_char(buydate,'D'))
-
could you explain why you chose this approach?
-
Originally Posted by umair123
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.
-
thanks...but would you please also explain why you chose that approach? thanks
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|