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
 Search by price with decimal datatype.

Author  Topic 

hspatil31
Posting Yak Master

182 Posts

Posted - 2013-05-25 : 02:00:12
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

shettybhas
Starting Member

3 Posts

Posted - 2013-05-25 : 03:31:30
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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-25 : 09:24:31
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
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -