2

I have created a query which gets results between 2 dates, Its working fine.. Now i have a scenario in which users does not enter any date..In this case I need to fetch all the result from database regardless of date.. The following query fails when user does not enter any date, it returns empty result set.
Kindly guide me how to resolve this problem.

select * from emp_register
where date between ' 2015-03-26 15:42:52' and ' 2015-03-26 15:42:52' 
or status= 'Y'

Date Format is Timestamp

3
  • Mysql or sqlserver ? And what the datatype for date ? Commented Jun 29, 2015 at 7:49
  • i have a scenario in which users does not enter any date Does it works with another language than MySQL ? Commented Jun 29, 2015 at 7:52
  • date format is Timestamps..I have edited my question Commented Jun 29, 2015 at 7:52

2 Answers 2

2

where date between ' 2015-03-26 15:42:52' and ' 2015-03-26 15:42:52'

' 2015-03-26 15:42:52' is NOT a DATE, it is a string. Never ever rely on implicit data type conversion. You might just be lucky to get the result depending on your NLS settings, however, it will certainly fail when the client NLS settings will be different for others.

Now i have a scenario in which users does not enter any date..In this case I need to fetch all the result from database regardless of date

No need to do in PL/SQL, do it in SQL.

Use NVL function on the values. Use a default lower bound and upper bound date values in the BETWEEN clause for the NVL.

For example,

SELECT *
FROM emp_register
WHERE DATE BETWEEN NVL(:date1, DATE '1900-01-01') AND NVL(:date2, DATE '9999-01-01')
OR status= 'Y'

So, whenever :date1 and/or :date2 and/is NULL, then it would use the default date values to fetch all the rows from the table.

Sign up to request clarification or add additional context in comments.

Comments

1

There are couple of options:

  1. Using Dynamic SQL to build your queries based on user input
  2. Using multiple IF/ELSE statements to build your query based on user input
  3. Setting default values for start and end dates that would always fit in your time range, such as 1900-01-01 and 2300-01-01 if user didn't pass any.

6 Comments

Thank you, I would go with 3rd Option :)
It's probably easiest and most readable. However, I personally think 2nd one would perform better (from my own experience), especially if your data grows fast. Let me know if this works for you. :)
sure,I will let you know.. Thanks :)
No need to use PL/SQL, do it in SQL using NVL.
@LalitKumarB That's right. You can use NVL to replace NULL values or define default parameters in procedure description, such as PROCEDURE P1 (Param1 IN NUMBER := 100); So it's either users' input or default value. Up to personal taste or/and SQL standarts in your own company I guess.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.