Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 WHERE clause

Author  Topic 

rongrace
Starting Member

13 Posts

Posted - 2012-03-07 : 09:29:10
I have a form that returns order details based on the input received in 4 parameters
using the following WHERE clause

WHERE HOrder.Acno = @Acno and
SUBSTRING(HDespatch.ArcOrdno,1,4) = @Unit and
SUBSTRING(HDespatch.ArcOrdno,5,8) >= @FromDate and
SUBSTRING(HDespatch.ArcOrdno,5,8) <= @ToDate

The current WHERE clause expects all these fields to contain data, but I want to expand the clause so that it will cater for the following.

If the Acno is blank then it will return all the orders for the unit within the date range.
If the unit is blank it will return all orders for the Acno for the date range entered.
If the date range is blank it will return all the Acno's orders for that unit

Hope this makes sense and any help would be much appreciated

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-07 : 09:40:16
see here:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rongrace
Starting Member

13 Posts

Posted - 2012-03-07 : 09:43:23
Thanks for that, I'll have a shufty
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-07 : 19:07:17
why is value stored in a concatenated format in field? why not maintain date value in its own field?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rongrace
Starting Member

13 Posts

Posted - 2012-03-08 : 04:50:57
Data came from a legacy system in that format
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-03-08 : 08:30:56
so change it to a real date. that is the best part of having a new system.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-08 : 10:15:44
quote:
Originally posted by rongrace

Data came from a legacy system in that format


just keep in mind that persisting with it will really make manipulations complex. everywhere you use the individual parts you've to apply SUBSTRING functions and get them which will also have an adverse effect on performance too.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -