Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with syntax 1

Status
Not open for further replies.

Cpreston

MIS
Mar 4, 2015
972
GB
Hi

I have this bit of code which keeps giving me errors, I have tried all syntax or this I have and currently have the error An expression of non-Boolean type specified in a contect where a condition is expected near order.

select * from [148-vwPacksSoldbyGroup p3] with (nolock) where 'DateRequired between %Date Range% and %Enter Date To%' order by DateRequired desc

Basically I am trying to get this to run and ask for 2 dates to be entered by the person so it list all rows found between the two dates.

Any idesa please
 
Cpreston

I have a table with a field Mydate and the data type is Date.

Not sure what language you have on top of you SQL but

If want a specific date use

Select * from MyTable Where mydate = ('05/15/2014')

If I want a range I use this.

Select * from MyTable Where mydate between ('05/21/2014') and ('05/21/2014')


Hope this helps

 
The issue is that this is a string:

'DateRequired between %Date Range% and %Enter Date To%'

So SQL Server has nothing to do a comparison with. That's the first part of the issue. The next part is that your values to compare against seem to be conflicting. One is a date range and the other is a date. You can't do a between that way. For example: a date range might be 10 days. You can't do: Where DateRequired is between 10 days and 2015-03-05 05:00. (Using 2015-03-05 05:00 as the Enter Date To value.

An example to show how the where clause would work is:

Code:
select * 
from [148-vwPacksSoldbyGroup p3] with (nolock) 
where DateRequired between 'BeginDate' and 'Enddate'
 order by DateRequired desc

I know that probably doesn't meet your requirements, but you didn't tell us what the requirement is or provide examples (sample data as is and how you want it returned). If you are looking to get n days before the Enter Date To value, you could do something like:

WHERE DateRequired BETWEEN 'Enter Date To' AND ('Enter Date To' - @DateRange)

But you would have to supply the @DateRange value as a parameter or hardcode it in.

(BTW-I do notice you have the values in the WHERE clause surrounded by wildcards %. You are doing this in Microsoft SQL Server aren't you? That is what this forum is for).

-SQLBill


-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top