1
votes

I am trying to input a date value from the user and then using that value in the query.

 select * from  TB_MNP_GTY_TRANS_STEPS where CREATE_DATETIME>=&startdate

Now when i run the sql statement in Toad and input 8/1/2012 as date data type i am getting

ORA-00932: inconsistent datatypes: expected DATE got NUMBER

Can someone suggest where i am wrong.Note that CREATE_DATETIME is of Date Type.

2

2 Answers

2
votes

You should really specify what date format you are using in your parameter:

SELECT *
  FROM TB_MNP_GTY_TRANS_STEPS 
 where CREATE_DATETIME >= TO_DATE(&startdate, 'DD/MM/YYYY');

Read about date formats here

Currently your session is expecting the date to be in its default NLS_DATE default fomat and obviously the format of the date you're entering is different. Explicitly specifying date formats prevents this issue from occurring.

Hope it helps...

EDIT: If you want to pass in the 8th January 2012 then you could specify your variable value as:

08/01/2012

And your select would be:

SELECT *
  FROM TB_MNP_GTY_TRANS_STEPS 
 where CREATE_DATETIME >= TO_DATE(&startdate, 'DD/MM/YYYY');

Depending upon your environment you might need to wrap the variable in single quotes (for TOAD you definiely will) i.e.

SELECT *
  FROM TB_MNP_GTY_TRANS_STEPS 
 where CREATE_DATETIME >= TO_DATE('&startdate', 'DD/MM/YYYY');

The error you are getting is caused by the format of the date string you are entering not matching EXACTLY the format you are specifying (see the leading "0" before the 8 and 1 in the day and month!)

1
votes

Date casting necessary

select * from  TB_MNP_GTY_TRANS_STEPS where CREATE_DATETIME>=to_date(&startdate, 'MM-DD-YYYY')

and while passing parameter you should pass value in quoets as '08-09-1999'