开发者

Oracle SQL - How do i output data from a table based on the day of the week from a hiredate column?

To be specific: I have a table with lets day 6 columns, one of which is Hiredate. the Hiredate is of the the type DATE and in the form of dd-Mon-YY. Now I am trying to only output the rows of data which have a hiredate of Monday or Tuesday.

Currently i am trying to do something along the lines of:

SELECT name, position, hiredate
FROM table
WHERE hiredate = 'monday';

or

SELECT TO_CHAR(hiredate, 'day') 
FROM table
WHERE TO_CH开发者_StackOverflow社区AR(hiredate, 'day') = 'monday';

neither of these work, and neither do any of the other variations of SQL that I have tried.

I'm fairly certain this is extremely simple, but I am a beginner and am kind of lost on where to go. Any help or tips would be appreciated.


Hoons's answer is correct, but will only work if your Oracle session is using English language (NLS_LANGUAGE).

Another query that work for all languages is

select name, position, hiredate
 from table
where to_char(sysdate, 'D') in (1, 2); -- 1 monday; 2 tuesday

to_char(sysdate, 'D') returns the following values for each day of week:

1 monday
2 tuesday
3 wednesday
4 thrusday
5 friday
6 saturday
7 sunday


In general, I have found Oracle's own 10g docs invaluable, you can find them here: Oracle 10g Doc Homepage, and in this case searching for day of week yields some good results, like this one: enter link description here, on that page, I see: DAY Yes Name of day, padded with blanks to display width of the widest name of day in the date language used for this element.

So I'm thinking that you're probably getting back Monday, but padded with spaces afterwards. I would probably try to get the abbreviated form, something like:

SELECT TO_CHAR(hiredate, 'DY') FROM table WHERE lower(TO_CHAR(hiredate, 'DY')) in ('mon', 'tue');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜