3

I have a table which looks like:

DAYDATE     TIMEOFDAY   USERID
18-JUL-12   Afternoon   mistu4u
18-JUL-12   Noon        mistu4u
20-JUL-12   Noon        mistu4u
21-JUL-12   Afternoon   mistu4u
11-SEP-12   Afternoon   rimo
06-AUG-12   Noon        rimo
14-JUN-12   Noon        mistu4u

Now I want to fetch those values for mistu4u whose daydate's month is 'jul'.I tried like

select * from mealdb where userid='mistu4u' and substr(daydate,4,3)='jul';

But it is giving 'no data found'.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Mistu4u
  • 5,132
  • 15
  • 53
  • 91

5 Answers5

3

Is your install case sensitive? Does this SQL work?

select * from mealdb where userid='mistu4u' and substr(daydate,4,3)='JUL';
ProfessionalAmateur
  • 4,447
  • 9
  • 46
  • 63
  • OMG!!It is!!It just worked!!But sure I want to make it case insensitive.@ProfessionalAmateur can you tell me how to make it so?Also can you suggest how to write it with "LIKE". – Mistu4u Jul 19 '12 at 13:43
  • @Subir Adhikari - It can be done. Check [here](http://stackoverflow.com/questions/5391069/make-oracle-sql-like-query-case-insensitive) – ProfessionalAmateur Jul 19 '12 at 14:01
0

Try

SELECT *
FROM   mealdb
WHERE  userid = 'mistu4u'
AND    TO_NUMBER(TO_CHAR(daydate,'MM')) = 7
;
Tebbe
  • 1,372
  • 9
  • 12
0

try

TRUNC(daydate, 'MONTH') = '01-JULY-2012'

Be careful if you have any indexes on the daydate column. The substr will render it useless. Perhaps, consider using a BETWEEN instead.

Also, most of the solutions will pick up all records in any year for the month of July.

OraNob
  • 684
  • 3
  • 9
0

Try with this:

select * from mealdb where userid='mistu4u' and daydate like '%JUL%';
Zeb
  • 1,715
  • 22
  • 34
HElper
  • 1
0
select *
from mealdb
where userid='mistu4u'
and upper(daydate) LIKE '%JUL%';
Imran
  • 171
  • 1
  • 5