I can't seem to make my SP works:
ALTER PROCEDURE [dbo].[TestSP]
#participant_role nvarchar(max) = null,
#active int,
#inactive int
AS
BEGIN
select * from participant_list where participant_role = ISNULL(#participant_role, participant_role)
and
case when #active = 0 then ((effective_end_date IS NULL) or (effective_end_date > CURRENT_DATE))
case when #inactive = 0 then ((effective_end_date IS NOT NULL) or (effective_end_date < CURRENT_DATE))
ELSE
--Return everything
END
What happen is, if the #active is 0, the query should include the condition ((effective_end_date IS NULL) or (effective_end_date > CURRENT_DATE))
Like wise, if #inactive is 0, the condition should include ((effective_end_date IS NOT NULL) or (effective_end_date < CURRENT_DATE))
And if both #active and #inactive are NOT 0, then it should return everything (no condition for this part). How do I make it work?
There seems to be a particular affliction where people discover SQL's CASE expression and seem to immediately stop thinking about simpler boolean operators:
select * from participant_list
where participant_role = ISNULL(#participant_role, participant_role)
and
(
(
#active = 0 and (
(effective_end_date IS NULL) or
(effective_end_date > CURRENT_DATE)
)
) or
(
#inactive = 0 and effective_end_date < CURRENT_DATE
) or
( #active = 1 and #inactive = 1)
)
SELECT *
FROM mytable
WHERE ...
AND
CASE
WHEN #active = 0 AND ((effective_end_date IS NULL) or (effective_end_date > CURRENT_DATE)) THEN
1
WHEN #inactive = 0 AND ((effective_end_date IS NOT NULL) or (effective_end_date < CURRENT_DATE)) THEN
1
ELSE
1
END = 1
select * from participant_list
where participant_role = ISNULL(#participant_role, participant_role)
and
(
(#active = 0 and (effective_end_date IS NULL or effective_end_date > CURRENT_DATE))
or
(#inactive = 0 and (effective_end_date IS NOT NULL or effective_end_date < CURRENT_DATE))
or
(#active != 0 and #active != 0)
)
using case
select * from participant_list
where (#participant_role IS NOT NULL AND participant_role = #participant_role)
and 1 = (case when #active = 0 AND (effective_end_date IS NULL or (effective_end_date > GETDATE()))
then 1
when #inactive = 0 AND (effective_end_date IS NOT NULL or (effective_end_date < GETDATE()) )
then 1
WHEN #active = 0 AND #inactive = 0
THEN 1
else 0 end)
Related
Is there a way to return nothing (not zero) if a count in SQL = 0?
My code:
(
SELECT COUNT(Id)
FROM t_Id pm1
WHERE
pm1.id2 = M.Mid1 AND
pm.id3 = pm1.ProductId AND
pm1.Removed = 0 AND
pm1.StartDate >= #StartDate AND
pm1.StartDate < #EndDate AND
ms.Status = 'L'
) AS TotalAccounts
If this query returns Zero, how can I return nothing (not zero), say when COUNT(id) > 0?
(
SELECT IIF(COUNT(Id) = 0, NULL, COUNT(Id))
FROM t_Id pm1
WHERE
pm1.id2 = M.Mid1 AND
pm.id3 = pm1.ProductId AND
pm1.Removed = 0 AND
pm1.StartDate >= #StartDate AND
pm1.StartDate < #EndDate AND
ms.Status = 'L'
) AS TotalAccounts
I have a script that does some validation, then updates some columns with the results.In the result, there is an error message column which is meant to be a human readable error message. Since there is more than one column that could be invalid, the error message can be different.
I did the below using a varible, but I think that using a variable is not a good idea here. Is there a better way to do this?
declare #ErrorMessage nvarchar(100);
;WITH _UPDATE_ as
(
Select CsrNum
,Substring(CsrNum, 2, 10) as CdsCsrId
,IsNumeric(Substring(CsrNum, 2, 10)) as [CdsCsrIdIsNumeric]
,case when IsNumeric(Substring(CsrNum, 2, 10)) = 1 then (Select CsrId from Csr c where c.CdsCsrId = Substring(l.CsrNum, 2, 10))
Else null
end as ConvertedCsrId
,PortalCsrid
,PlankLoadStatusId
,ErrorMessage
,PlankLoadStatusDate
,(Select Code from State where Code = l.OrderCust_StateCode) as ValidatedStateCode
from OrdercustPlankLoad l
Where PlankLoadStatusId = 1
)
Update _UPDATE_ set
#ErrorMessage += case when ValidatedStateCode is null then 'StateCode is invalid; ' else null end
,#ErrorMessage += case when ConvertedCsrId is null then 'CsrNum is Invalid; ' end
,ErrorMessage = #ErrorMessage
, PortalCsrId = ConvertedCsrId
, PlankLoadStatusId = case when #ErrorMessage is not null
then 4 --Error
else 2 -- Validated
end
,PlankLoadStatusDate = GetUTCDate()
What about trying something like this:
;WITH _UPDATE_ AS (
SELECT
CsrNum
, SUBSTRING ( CsrNum, 2, 10 ) AS CdsCsrId
, ISNUMERIC ( SUBSTRING ( CsrNum, 2, 10 ) ) AS [CdsCsrIdIsNumeric]
, CASE
WHEN ISNUMERIC ( SUBSTRING ( CsrNum, 2, 10 ) ) = 1 THEN
( SELECT CsrId FROM Csr c WHERE c.CdsCsrId = SUBSTRING ( l.CsrNum, 2, 10 ) )
ELSE NULL
END AS ConvertedCsrId
, PortalCsrid
, PlankLoadStatusId
, ErrorMessage
, PlankLoadStatusDate
, ( SELECT Code FROM [State] WHERE Code = l.OrderCust_StateCode ) AS ValidatedStateCode
FROM OrdercustPlankLoad l
WHERE
PlankLoadStatusId = 1
)
UPDATE _UPDATE_
SET
ErrorMessage = CASE
WHEN ValidatedStateCode IS NULL AND ConvertedCsrId IS NULL THEN 'StateCode is invalid; CsrNum is Invalid;'
WHEN ValidatedStateCode IS NULL THEN 'StateCode is invalid;'
WHEN ConvertedCsrId IS NULL THEN 'CsrNum is Invalid;'
ELSE NULL
END
, PortalCsrId = ConvertedCsrId
, PlankLoadStatusId = CASE
WHEN ValidatedStateCode IS NULL OR ConvertedCsrId IS NULL THEN 4 --Error
ELSE 2 -- Validated
END
, PlankLoadStatusDate = GETUTCDATE();
ALTER PROCEDURE GetVendor_RMA_CreditMemo
(#HasCreditMemoNo INT)
BEGIN
SELECT
*
FROM
(SELECT
CreditMemoNumber,
CASE WHEN CreditMemoNumber != ''
THEN 1
ELSE 0
END AS HasCreditMemoNo
FROM
XYZ) as C
WHERE
(C.HasCreditMemoNo = #HasCreditMemoNo OR #HasCreditMemoNo = -1)
END
CreditMemoNumber is a varchar column
I want to achieve this:
CASE
WHEN #HasCreditMemoNo = 0
THEN -- select all rows with no value in CreditMemoNumber Column,
WHEN #HasCreditMemoNo = 1
THEN -- all rows that has some data,
WHEN #HasCreditMemoNo = -1
THEN -- everything regardless..
You can't do this kind of thing with a CASE.
The correct way to do it is with OR:
WHERE (#HasCreditMemoNo = 0 AND {no value in CreditMemoNumber Column})
OR
(#HasCreditMemoNo = 1 AND {all rows that has some data})
OR
(#HasCreditMemoNo = -1)
Would this work for you? I'm not sure if it would improve your performance. You may be better off writing an if else if else statement and three separate select statements with an index on the CreditMemoNumber column.
ALTER PROCEDURE GetVendor_RMA_CreditMemo(#HasCreditMemoNo int)
BEGIN
select
CreditMemoNumber,
case when CreditMemoNumber != '' then 1 else 0 end as HasCreditMemoNo
from XYZ
where
(#HasCreditMemoNo = 0 and (CreditMemoNumber is null or CreditMemoNumber = ''))
or (#HasCreditMemoNo = 1 and CreditMemoNumber != '')
or (#HasCreditMemoNo = -1)
END
I'm working on an advanced search sproc and would like to know if it is possible for a portion of my select to be included or not based on a parameter. I could use an IF ELSE on my parameter, but I already have one for another parameter, and that seems like a lot of code duplication.
-- My advanced search function allows a user to specify various parameters. The basic search can be EXACT or not (contains vs freetext - first if), and other parameters (AND) can be specified. Specific keywords can also be selected as well (intersect).
My issue is that when #Keywords is null, I don't want to include the final INTERSECT SELECT ... portion of the code at the bottom of my example. Is there a quick way to do this without adding another IF ELSE in the top and bottom queries? Let me know if you need more detailed information.
declare #SearchTerms nvarchar(4000)
declare #GalleryId int
declare #Keywords nvarchar(4000)
declare #ExactWord int
declare #BeginDateUpload datetime
declare #EndDateUpload datetime
declare #BeginDateTaken datetime
declare #EndDateTaken datetime
declare #MinFileSize int
declare #MaxFileSize int
declare #AlbumType bit
declare #ImageType int
declare #AudioType int
declare #OtherType int
declare #VideoType int
set #SearchTerms = 'tulips'
set #GalleryId = 1
set #Keywords = null -- 'st-jean'
set #ExactWord = null
set #BeginDateUpload = null
set #EndDateUpload = null
set #BeginDateTaken = null
set #EndDateTaken = null
set #MinFileSize = null
set #MaxFileSize = null
set #AlbumType = 1
set #ImageType = 1
set #AudioType = 1
set #OtherType = 1
set #VideoType = 1
IF ISNULL(#ExactWord, 0) = 1
BEGIN
[... snip ...]
END
ELSE
select t1.* from (
SELECT 'm' as objType, m.MediaObjectId
FROM gs_mediaObjectMetadata md
INNER JOIN dbo.[gs_MediaObject] m
ON md.FKMediaObjectId = m.MediaObjectId
INNER JOIN dbo.[gs_Album] a
ON a.AlbumId = m.FKAlbumId
WHERE FREETEXT (value, #SearchTerms)
AND a.FKGalleryId = #GalleryId
AND (m.DateAdded >= ISNULL(#BeginDateUpload, m.DateAdded))
AND (m.DateAdded <= ISNULL(#EndDateUpload, m.DateAdded))
AND (m.DateTaken is NULL OR m.DateTaken >= ISNULL(#BeginDateTaken, m.DateTaken))
AND (m.DateTaken is NULL OR m.DateTaken <= ISNULL(#EndDateTaken, m.DateTaken))
AND (m.OriginalSizeKB >= ISNULL(#MinFileSize, m.OriginalSizeKB))
AND (m.OriginalSizeKB <= ISNULL(#MaxFileSize, m.OriginalSizeKB))
AND((m.FKMediaObjectTypeId = ISNULL(#ImageType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#AudioType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#VideoType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#OtherType, 0)))
union
SELECT 'm' as objType, m.MediaObjectId
FROM dbo.[gs_MediaObject] m
INNER JOIN dbo.[gs_Album] a
ON a.AlbumId = m.FKAlbumId
WHERE FREETEXT ((m.Title, OriginalFilename), #SearchTerms)
AND a.FKGalleryId = #GalleryId
AND (m.DateAdded >= ISNULL(#BeginDateUpload, m.DateAdded))
AND (m.DateAdded <= ISNULL(#EndDateUpload, m.DateAdded))
AND (m.DateTaken is NULL OR m.DateTaken >= ISNULL(#BeginDateTaken, m.DateTaken))
AND (m.DateTaken is NULL OR m.DateTaken <= ISNULL(#EndDateTaken, m.DateTaken))
AND (m.OriginalSizeKB >= ISNULL(#MinFileSize, m.OriginalSizeKB))
AND (m.OriginalSizeKB <= ISNULL(#MaxFileSize, m.OriginalSizeKB))
AND((m.FKMediaObjectTypeId = ISNULL(#ImageType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#AudioType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#VideoType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#OtherType, 0)))
) t1
--IF #Keywords != null -- conditional intersect
intersect
SELECT 'm' as objType, m.MediaObjectId
FROM dbo.[gs_MediaObject] m
INNER JOIN dbo.[gs_Album] a
ON a.AlbumId = m.FKAlbumId
left join dbo.gs_MediaObjectMetadata md
on m.MediaObjectId = md.FKMediaObjectId
WHERE FREETEXT ((m.Title, OriginalFilename), #SearchTerms)
AND a.FKGalleryId = #GalleryId
AND (m.DateAdded >= ISNULL(#BeginDateUpload, m.DateAdded))
AND (m.DateAdded <= ISNULL(#EndDateUpload, m.DateAdded))
AND (m.DateTaken is NULL OR m.DateTaken >= ISNULL(#BeginDateTaken, m.DateTaken))
AND (m.DateTaken is NULL OR m.DateTaken <= ISNULL(#EndDateTaken, m.DateTaken))
AND (m.OriginalSizeKB >= ISNULL(#MinFileSize, m.OriginalSizeKB))
AND (m.OriginalSizeKB <= ISNULL(#MaxFileSize, m.OriginalSizeKB))
AND((m.FKMediaObjectTypeId = ISNULL(#ImageType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#AudioType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#VideoType, 0))
OR (m.FKMediaObjectTypeId = ISNULL(#OtherType, 0)))
and UPPER(md.Description) = 'KEYWORDS'
and exists (
SELECT *
FROM dbo.fnSplit(Replace(md.Value, '''', ''''''), ',') split
WHERE split.item in
(SELECT * from dbo.fnSplit(Replace(#Keywords, '''', ''''''), ','))
)
Thank you
What about instead of doing the intersect, try this out in your inner queries : #Keyword is null or .....
and ( #Keywords is null or (UPPER(md.Description) = 'KEYWORDS'
and exists (
SELECT *
FROM dbo.fnSplit(Replace(md.Value, '''', ''''''), ',') split
WHERE split.item in
(SELECT * from dbo.fnSplit(Replace(#Keywords, '''', ''''''), ','))
)
)
)
If anyone can help me on this I would be very excited...I've already spent about 4 hours and cannot find out why T-SQL gives me wrong results and the same query in sql is fine(I mean only #ZipCodeIDS is not null, that's why I put null for the rest of vars in plain sql).
T-SQL Query:
-- validate the reference number pattern
DECLARE #PCodePattern nvarchar(130)
IF #PCode = '' SET #PCode = NULL
IF #PCode IS NOT NULL
BEGIN
IF LTRIM(RTRIM(#PCode)) <> ''
BEGIN
-- filter by pattern
SELECT #PCodePattern = #PCode
END
END -- #PCode
if (#strAddress is not null)
set #strAddress = '%' + #strAddress + '%'
Declare #listofIDS table(zipcodeids int)
delete from #listofIDS
IF #ZipCodeIDS = '' SET #ZipCodeIDS = NULL
IF #ZipCodeIDS IS NOT NULL
BEGIN
IF CHARINDEX(',', #ZipCodeIDS) = 0
BEGIN
insert #listofIDS values(#ZipCodeIDS)
END
ELSE
BEGIN
set #ZipCodeIDS = #ZipCodeIDS + ','
WHILE CHARINDEX(',', #ZipCodeIDS) <> 0
BEGIN
insert #listofIDS values(Left(#ZipCodeIDS, CHARINDEX(',',#ZipCodeIDS) - 1))
SET #ZipCodeIDS = SUBSTRING(#ZipCodeIDS, CHARINDEX(',',#ZipCodeIDS) + 1, LEN(#ZipCodeIDS) - CHARINDEX(',',#ZipCodeIDS))
END
END
END
-- select the property data
INSERT INTO #PropertyDetails (PropertyID, PCode, PropertyStatusID, PropertyStatusName,
PropertyTypeID, PropertyTypeName, ResearchStatusID,
ResearchStatusName, FullAddress, PartialAddress, Address1,
Address2, ZipCodeID, ZipCode, ZipCodeDescription, CityID,
CityName, StateID, StateName, StateCode, NumBedrooms, NumBathrooms,
LivingSquareFeet, LotSquareFeet, YearBuilt, ZillowLink,
AssessorParcelNumber, DateWentREO, DateAppearedOnMLS, IsOnTheMLS,
ZPropertyID, LowestPrice, HighestPrice, AskingPrice, DateTimeRecorded,
RecordedByPersonID, RecordedByPersonName, AssignedToPersonID,
AssignedToPersonName, WatchTag, Latitude, Longitude)
SELECT p.PropertyID, p.PCode, p.PropertyStatusID, ps.Name, p.PropertyTypeID, pt.Name,
p.ResearchStatusID, rs.Name, dbo.GetAddress(p.PropertyID),
dbo.GetPartialAddress(p.PropertyID), p.Address1, p.Address2, p.ZipCodeID, z.Code,
z.Description, p.CityID, c.Name, p.StateID, s.Name, s.Code, p.NumBedrooms,
p.NumBathrooms, p.LivingSquareFeet, p.LotSquareFeet, p.YearBuilt, p.ZillowLink,
p.AssessorParcelNumber, p.DateWentREO, p.DateAppearedOnMLS, p.IsOnTheMLS,
p.ZPropertyID, p.LowestPrice, p.HighestPrice, p.AskingPrice, p.DateTimeRecorded,
p.RecordedByPersonID, dbo.GetDisplayName(p.RecordedByPersonID), p.AssignedToPersonID,
dbo.GetDisplayName(p.AssignedToPersonID), w.WatchTag, p.latitude, p.longitude
FROM Properties p
JOIN cfgPropertyStatuses ps
ON ps.PropertyStatusID = p.PropertyStatusID
JOIN cfgPropertyTypes pt
ON pt.PropertyTypeID = p.PropertyTypeID
JOIN cfgResearchStatuses rs
ON rs.ResearchStatusID = p.ResearchStatusID
JOIN ZipCodes z
ON z.ZipCodeID = p.ZipCodeID
JOIN cfgStates s
ON s.StateID = p.StateID
LEFT JOIN cfgCities c
ON c.CityID = p.CityID
LEFT JOIN Watches w
ON w.PropertyID = p.PropertyID AND w.PersonID = #LoggedInPersonID
WHERE /*
******* missing filter *******
this line should filter the risks by #LoggedInPersonID via role
******************************
AND */(#PropertyID IS NULL OR p.PropertyID = #PropertyID)
AND (#PCodePattern IS NULL OR p.PCode LIKE #PCodePattern)
AND (#ZipCodeIDS IS NULL
OR p.ZipCodeID IN (select zipcodeids from #listofIDS))
AND (#NumBedroomsFrom IS NULL OR (p.NumBedrooms >= #NumBedroomsFrom
AND #NumBedroomsTo IS NOT NULL
AND p.NumBedrooms <= #NumBedroomsTo)
OR (p.NumBedrooms = #NumBedroomsFrom
AND #NumBedroomsTo IS NULL))
AND (#NumBedroomsTo IS NULL OR (p.NumBedrooms <= #NumBedroomsTo
AND (#NumBedroomsTo IS NULL OR p.NumBedrooms <= #NumBedroomsTo)))
AND (#LivingSizeFrom IS NULL OR (p.LivingSquareFeet >= #LivingSizeFrom))
AND (#LivingSizeTo IS NULL OR (p.LivingSquareFeet <= #LivingSizeTo))
AND (#LotSizeFrom IS NULL OR (p.LotSquareFeet >= #LotSizeFrom))
AND (#LotSizeTo IS NULL OR (p.LotSquareFeet <= #LotSizeTo))
AND
/* if status is null, return all. Or, return only statuses that are passed in */
(#PropertyStatuses IS NULL or
((p.PropertyStatusID=#PropertyStatuses and (p.PropertyStatusID & (32 | 128)) = 0) or
#PropertyID is not null or #PCode is not null) or
(p.PropertyStatusID = (p.PropertyStatusID & #PropertyStatuses)))
/*
-- return the property if the specific ID was given otherwise ommit Sold and Archived
AND ((p.PropertyStatusID & (32 /*sold*/ | 128 /*archived*/)) = 0
OR #PropertyID IS NOT NULL
OR #PCode IS NOT NULL))
OR (p.PropertyStatusID = (p.PropertyStatusID & #PropertyStatuses)))
*/
AND (#PropertyTypes IS NULL
OR (p.PropertyTypeID = (p.PropertyTypeID & #PropertyTypes)))
AND (#ResearchStatuses IS NULL
OR (p.ResearchStatusID = (p.ResearchStatusID & #ResearchStatuses)))
AND (#IsOnTheMLS IS NULL OR p.IsOnTheMLS = #IsOnTheMLS)
and (#strAddress is null or (p.Address1 LIKE #strAddress or p.Address2 LIKE #strAddress))
RETURN
and the same, translated by me in SQL (which works good):
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [PropertyID]
,[PCode]
,[Address1]
,[Address2]
,[NumBedrooms]
,[NumBathrooms]
,[LivingSquareFeet]
,[LotSquareFeet]
,[YearBuilt]
,[ZillowLink]
,[AssessorParcelNumber]
,[DateWentREO]
,[DateAppearedOnMLS]
,[IsOnTheMLS]
,[ZPropertyID]
,[LowestPrice]
,[HighestPrice]
,[AskingPrice]
,[DateTimeRecorded]
,[RecordedByPersonID]
,[AssignedToPersonID]
,[latitude]
,[longitude]
,[Zestimate]
FROM [dev_hotsheetDB].[dbo].[Properties] p
JOIN [dev_hotsheetDB].[dbo].cfgPropertyStatuses ps
ON ps.PropertyStatusID = p.PropertyStatusID
JOIN [dev_hotsheetDB].[dbo].cfgPropertyTypes pt
ON pt.PropertyTypeID = p.PropertyTypeID
JOIN [dev_hotsheetDB].[dbo].cfgResearchStatuses rs
ON rs.ResearchStatusID = p.ResearchStatusID
JOIN [dev_hotsheetDB].[dbo].ZipCodes z
ON z.ZipCodeID = p.ZipCodeID
JOIN [dev_hotsheetDB].[dbo].cfgStates s
ON s.StateID = p.StateID
LEFT JOIN [dev_hotsheetDB].[dbo].cfgCities c
ON c.CityID = p.CityID
where
(NULL IS NULL OR p.PropertyID = NULL)
AND (NULL IS NULL OR p.PCode LIKE NULL)
AND ('1' IS NULL
OR p.ZipCodeID IN (select zipcodeids from [dev_hotsheetDB].[dbo].[listofIDS]))
AND (NULL IS NULL OR (p.NumBedrooms >= NULL
AND NULL IS NOT NULL
AND p.NumBedrooms <= NULL)
OR (p.NumBedrooms = NULL
AND NULL IS NULL))
AND (NULL IS NULL OR (p.NumBedrooms <= NULL
AND (NULL IS NULL OR p.NumBedrooms <= NULL)))
AND (NULL IS NULL OR (p.LivingSquareFeet >= NULL))
AND (NULL IS NULL OR (p.LivingSquareFeet <= NULL))
AND (NULL IS NULL OR (p.LotSquareFeet >= NULL))
AND (NULL IS NULL OR (p.LotSquareFeet <= NULL))
AND
/* if status is null, return all. Or, return only statuses that are passed in */
(NULL IS NULL or
((p.PropertyStatusID=NULL and (p.PropertyStatusID & (32 | 128)) = 0) or
NULL is not null or NULL is not null) or
(p.PropertyStatusID = (p.PropertyStatusID & NULL)))
/*
-- return the property if the specific ID was given otherwise ommit Sold and Archived
AND ((p.PropertyStatusID & (32 /*sold*/ | 128 /*archived*/)) = 0
OR #PropertyID IS NOT NULL
OR #PCode IS NOT NULL))
OR (p.PropertyStatusID = (p.PropertyStatusID & #PropertyStatuses)))
*/
AND (NULL IS NULL
OR (p.PropertyTypeID = (p.PropertyTypeID & NULL)))
AND (NULL IS NULL
OR (p.ResearchStatusID = (p.ResearchStatusID & NULL)))
AND (NULL IS NULL OR p.IsOnTheMLS = NULL)
and (NULL is null or (p.Address1 LIKE NULL or p.Address2 LIKE NULL))
Please note that the issue is only related to IN statement... When #ZipCodeIDS = '1,2,3' it should return 414 results (plain sql ok) but T-SQL function returns 80..
The strange thing I've noticed is that T-SQL only takes in consideration FIRST id from #ZipCodeIDS (as you see I split these ids and put them into a temp table). So here's the issue - about this first id... (cannot confirm that this is the only issue, because there were times when for the first zipCodeId it shouldn't return anything but it still returned results)
Can you give a helping hand please?
Ps: for my plain sql query, I've used a real table with those ids, just to mimic the behavior as much as possible...
UPDATE: The splitting of the #ZipCodeIDS and insertion into the temp table works perfectly: https://data.stackexchange.com/stackoverflow/q/109406/
Solved the issue with Timbo help!!!!!
I've declared #ZipCodeIDS varchar and somehow it is made by default VARCHAR(1).
I changed it to #ZipCodeIDS varchar(50) and not it perfectly works!!!!
Sorry guys because this declaration was hidden from you!