Reputation: 3717
I want to return null value in case when. Here is my query
Declare @MasterProduct nvarchar(50) = N'Sharepoint Easy Nav',
@ProductSlug nvarchar(50) = 'sharepoint-easynav-free',
@CountryID bigint = 1111;
SELECT
Product.ProductName,
Product.MasterProductName,
Price.ProductPrice,
Price.ProductTax,
Price.ProductTotalPrice,
Product.TotalSiteCollectionText,
Product.TotalSiteCollection,
Product.Is_Support,
Price.Is_Support_Price,
Product.ProductSlug,
Country.CountrySymbol,
Country.CountryId
FROM
BR_Product as Product
Inner Join BR_ProductPrice as Price on Product.ID = Price.ProductID
left Join BR_Country as Country On Price.CountryID = Country.CountryId
where Product.MasterProductName = IsNull(@MasterProduct, Product.MasterProductName)
and Product.ProductSlug = IsNull(@ProductSlug, Product.ProductSlug)
and Country.CountryId = case
when (select count(BR_ProductPrice.ID)
from BR_ProductPrice
where BR_ProductPrice.CountryID = @CountryID) > 0
Then @CountryID
else Null
end
It returns me no rows.
When I remove
Country.CountryId = case when (select count(BR_ProductPrice.ID) from BR_ProductPrice where BR_ProductPrice.CountryID = @CountryID) > 0 Then @CountryID else Null end
I want CountryId compare null part of case statement in else section something like this
Then @CountryID else Country.CountryId is null
Query is working properly when I pass CountryID = 1101.
Upvotes: 1
Views: 2258
Reputation: 537
Use ISNULL(CountryID,'') on where conditions and on conditions and replace = null with = ''
Inner Join BR_ProductPrice as Price on Product.ID = Price.ProductID
left Join BR_Country as Country On isnull(Price.CountryID,'') = isnull(Country.CountryId,'')
where Product.MasterProductName = ISNULL(@MasterProduct, Product.MasterProductName)
and Product.ProductSlug = ISNULL(@ProductSlug, Product.ProductSlug)
and ISNULL(Country.CountryId,'') = case
when (select count(BR_ProductPrice.ID)
from BR_ProductPrice
where ISNULL(BR_ProductPrice.CountryID,'') = @CountryID) > 0
Then @CountryID
else ''
end
Upvotes: 1
Reputation: 4191
Try to change this
from:
Country.CountryId = case
when (select count(BR_ProductPrice.ID)
from BR_ProductPrice
where BR_ProductPrice.CountryID = @CountryID) > 0
Then @CountryID
else Null
end
To:
Country.CountryId =(select Case when cnt_BR_ProductPriceID > 0 then @CountryID else null end from
(select count(BR_ProductPrice.ID) as cnt_BR_ProductPriceID from BR_ProductPrice where BR_ProductPrice.CountryID = @CountryID) as cnt)
Never tried yet.
Hope it work.
Upvotes: 0
Reputation: 82474
You can't use =
with null
. Try this instead:
and (
(Country.CountryId = @CountryID
and (select count(BR_ProductPrice.ID)
from BR_ProductPrice
where BR_ProductPrice.CountryID = @CountryID) > 0
)
OR Country.CountryId IS NULL
)
Upvotes: 0