Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 order by weekdays

Author  Topic 

shaaquille
Starting Member

4 Posts

Posted - 2009-09-08 : 01:44:57
hi every body
m new to this forum and this is my 1st post
i have to write a query
"i have to take the day on which an employee was hired from hiredate and then i have to order it by weekdays like monday shd b 1st and so on " i have written this query bt it wrks fr wednesday only which is the 1st record...
--------------------------------
select ename,sal,hiredate,to_char(hiredate,'DAY'),
case to_char(hiredate,'DAY') when 'MONDAY' then 1
when 'TUESDAY' then 2
when 'WEDNESDAY' then 3
when 'THURSDAY' then 4
when 'FRIDAY' then 5
when 'SATURDAY' then 6
when 'SUNDAY' then 7
END DAY
FROM EMP order by DAY
------------------------------------
thnx in advance
regards !

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-09-08 : 02:42:41
Hi Try this


select ename,sal,hiredate, datepart(weekday,hiredate) as Day from EMP
order by datepart(weekday,hiredate)

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-08 : 02:43:18
What is to_char()?
I don't know in SQL Server...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-08 : 03:13:08
quote:
Originally posted by webfred

What is to_char()?
I don't know in SQL Server...


No, you're never too old to Yak'n'Roll if you're too young to die.


It is used in ORACLE which OP uses

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-08 : 03:15:37
quote:
Originally posted by shaaquille

hi every body
m new to this forum and this is my 1st post
i have to write a query
"i have to take the day on which an employee was hired from hiredate and then i have to order it by weekdays like monday shd b 1st and so on " i have written this query bt it wrks fr wednesday only which is the 1st record...
--------------------------------
select ename,sal,hiredate,to_char(hiredate,'DAY'),
case to_char(hiredate,'DAY') when 'MONDAY' then 1
when 'TUESDAY' then 2
when 'WEDNESDAY' then 3
when 'THURSDAY' then 4
when 'FRIDAY' then 5
when 'SATURDAY' then 6
when 'SUNDAY' then 7
END DAY
FROM EMP order by DAY
------------------------------------
thnx in advance
regards !


What is wrong with simple Order by hiredate?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

shaaquille
Starting Member

4 Posts

Posted - 2009-09-08 : 19:23:20
hi
thanx fr ur replies ....
nthing wrong vd the hiredate :)
actually i was preparing fr oracle sql course and it was there in the exercise...
and datepart is not wrking in oracle sql...
besides i havent read this function uptill now so thr must be a solution to it with simple functions ....
looking frward to ur replies .
thnx
regards !
Go to Top of Page

shaaquille
Starting Member

4 Posts

Posted - 2009-09-08 : 19:33:29
hi
well sm how i have managed to do it ...
------------------------
select ename,sal,hiredate,to_char(hiredate,'D'),to_char(hiredate,'DAY'),
case to_char(hiredate,'D') when '4' then 3
when '6' then 5
when '1' then 7
when '2' then 1
when '3' then 2
when '5' then 4
when '7' then 6
END dda
FROM EMP order by dda
-----------------------
it may help sm1 :)
thnx fr ur help !
regards !
Go to Top of Page
   

- Advertisement -