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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Search by price with decimal datatype.
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

182 Posts

Posted - 05/25/2013 :  02:00:12  Show Profile  Reply with Quote
Dear All,

I have following sql query for search record by price. I am passing through my code PriceFrom and PriceTo as DECIMAL. And DB have DECIMAL datatype.

Can anyone please help what condition have to add for IF condition?
Now I have added (<> ''). Sometimes I will pass 0 to 12 so 0 should be consider

SET @Sql='SELECT DISTINCT Id FROM [ProductVariant] PRDV' SET @Sql=@Sql +' WHERE' IF(convert(nvarchar(50), @PriceFrom) <> '' AND convert(nvarchar(50), @PriceTo) <> '') SET @Sql1=@Sql1 + ' PRDV.Price BETWEEN '+ convert(nvarchar(50), @PriceFrom) +' AND '+ convert(nvarchar(50), @PriceTo) +' AND'

Thanks and Regard's
Harish Patil

Edited by - hspatil31 on 05/25/2013 02:36:12

Starting Member

3 Posts

Posted - 05/25/2013 :  03:31:30  Show Profile  Reply with Quote
Try this....
SET @Sql = 'SELECT DISTINCT Id FROM [ProductVarian] '
SET @Sql += 'WHERE ISNULL(Price,0) BETWEEN ' + CONVERT(varchar(50), @PriceFrom) + ' AND ' + CONVERT(varchar(50),@PriceTo)

and later execute the @Sql using sp_executesql procedure.
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 05/25/2013 :  09:24:31  Show Profile  Reply with Quote
the usual way of doing this if you've multiple set of where conditions based on parameter values is as follows

SET @Sql='SELECT DISTINCT Id FROM [ProductVariant] PRDV WHERE 1 = 1 ' 

IF(convert(nvarchar(50), @PriceFrom) <> '' AND convert(nvarchar(50), @PriceTo) <> '') 
SET @Sql1=@Sql1 + ' AND PRDV.Price BETWEEN '+ convert(nvarchar(50), @PriceFrom) +' AND '+ convert(nvarchar(50), @PriceTo) 

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000