Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Want employee list of those whose start date was Mar-07?

M_ID M_NAME DEPT_ID START_DATE END_DATE Salary
M001 Richa D001 27-Jan-07 27-Feb-07 150000
M002 Nitin D002 16-Feb-07 16-May-07 40000
M003 AJIT D003 8-Mar-07 8-Sep-07 70000
M004 SHARVARI D004 28-Mar-07 28-Mar-08 120000
M005 ADITYA D002 27-Apr-07 27-Jul-07 40000
M006 Rohan D004 12-Apr-07 12-Apr-08 130000
M007 Usha D003 17-Apr-07 17-Oct-07 70000
M008 Anjali D002 2-Apr-07 2-Jul-07 40000
M009 Yash D006 11-Apr-07 11-Jul-07 85000
M010 Nalini D007 15-Apr-07 15-Oct-07 9999

I want to filter out employees that have start_date of Mar-07 I tried below query for the same but no rows got selected .Is my approach wrong?

SELECT * 
FROM EMP_TABLE
WHERE START_DATE LIKE '%Mar-07%';

Also my table structure is like this:

Name        Null? Type
M_ID              VARCHAR2(4)
M_NAME            VARCHAR2(20)
DEPT_ID           CHAR(4)
START_DATE        DATE
END_DATE          DATE
SALARY            NUMBER(6)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your current query:

SELECT * FROM EMP_TABLE WHERE START_DATE LIKE '%Mar-07%';

relies on implicit conversion of every date to a string using your session NLS settings, and then string comparison. While that could work, it looks like maybe your NLS settings don’t seem to match that format – PL/SQL Developer might be formatting the dates itself – or you’re actually seeing MAR-07 and aren’t matching the case properly.

You can see your NLS settings by selecting TO_CHAR(START_DATE), or by querying the nls_session_parameters view.

In any case, it would be better to search for a range of dates, rather than converting to strings:

SELECT * FROM EMP_TABLE
WHERE START_DATE >= DATE '2007-03-01'
AND START_DATE < DATE '2007-04-01'

Note that this will only find dates in 2007, while your original would include 1907, 1807 etc. – but that’s probably what you want really.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading