How to convert string array to int array in T-Sql - sql-server

I got a sql function that returns a string array,..., now I want to convert it into an int array,
Msg 245, Level 16, State 1, Line 14 Conversion failed when converting
the varchar value ',' to data type int.
My code :
DECLARE #ListZoneId as varchar(200);
SET #ListZoneId = [dbo].[CMS_fGetListOfZoneIdByParentZoneId](1)
//sql function return string array : '1,22,30,14'
declare #tempTb table
(
TempId int
)
while charindex(',',#ListZoneId) > 0
begin
insert into #tempTb select substring(#ListZoneId,1,1)
SET #ListZoneId = substring(#ListZoneId,3,len(#ListZoneId))
end
SELECT COUNT(*) from Shop as s inner join ShopInZone as z on s.Id=z.ShopId inner join Zone as zo on zo.Id = z.ZoneId
where z.ZoneId IN ( select * from #tempTb)

Related

Snowflake case statement to return multiple columns value in procedure

Below is the snowflake procedure in am trying to run.
CREATE OR REPLACE PROCEDURE test()
RETURNS VARCHAR(16777216)
LANGUAGE SQL
AS
$$
DECLARE
V_LAT varchar;
V_LNG varchar;
BEGIN
INSERT INTO test.crs_compact.case_test
(
c_address,
c_comment
)
SELECT
(CASE WHEN c_nationkey = 0 then (:V_LAT=a.c_address, :V_LNG=c_comment)
END)
from "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."CUSTOMER" as a;
return 'Data Loaded Successfully';
END;
$$
while calling the procedure getting below error.
Uncaught exception of type 'STATEMENT_ERROR' on line 11 at position 1 : SQL compilation error: error line 7 at position 3 Invalid argument types for function 'IFF': (BOOLEAN, ROW(BOOLEAN, BOOLEAN), NULL)
(a.c_address, c_comment) or (:V_LAT=a.c_address, :V_LNG=c_comment) is a ROW
you cannot put a ROW inside a CASE (which is also an IFF)..
Where you have typed looks like you are trying to do this:
SELECT
a.c_address, c_comment
from "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."CUSTOMER" as a
WHERE c_nationkey = 0;
Or more to the point:
INSERT INTO test.crs_compact.case_test
(
c_address,
c_comment
)
SELECT
a.c_address, c_comment
from "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."CUSTOMER" as a
WHERE c_nationkey = 0;
Now if you want those null values instead of skipping them:
INSERT INTO test.crs_compact.case_test
(
c_address,
c_comment
)
SELECT
iff(c_nationkey = 0, a.c_address, NULL)
,iff(c_nationkey = 0, c_comment, NULL)
from "SNOWFLAKE_SAMPLE_DATA"."TPCH_SF1"."CUSTOMER" as a;

Conversion failed when converting the varchar value '01-4001' to data type int

My stored procedure is as follows:
DECLARE #LOCATION_ID VARCHAR(MAX) = 3,
#INVENTORY_ITEM_ID VARCHAR(MAX) = '01-4001' ,
#CALLFROM VARCHAR(MAX) = 'FRAMES' --Requested by VIkram Dated 29jan 2020 For MBT :- 25401,25402
BEGIN
SET NOCOUNT ON;
BEGIN TRY
DECLARE #TABLE_LOCATION_ID TABLE
(
ID INT,
LOCATION_ID INT
)
INSERT INTO #TABLE_LOCATION_ID
SELECT *
FROM dbo.SPLIT(#LOCATION_ID, ',')
SELECT * FROM #TABLE_LOCATION_ID
DECLARE #TABLE_INVENTORY_ITEM_ID TABLE
(
ID INT,
INVENTORY_ITEM_ID VARCHAR(MAX) -- 2189
)
INSERT INTO #TABLE_INVENTORY_ITEM_ID
SELECT *
FROM dbo.SPLIT(#INVENTORY_ITEM_ID, ',')
SELECT * FROM #TABLE_INVENTORY_ITEM_ID
SELECT DISTINCT LOCATION_ID
FROM #TABLE_LOCATION_ID
IF('FRAMES' = 'FRAMES')
BEGIN
-- declare #LOCATION_ID int ,
--#INVENTORY_ITEM_ID VARCHAR(MAX);
SELECT DISTINCT
I.[UPC_ID],
I.INVENTORY_ITEM_ID,
IP.LOCATION_ID,
PL.LOCATION_NAME,
F.MANUFACTURER,
(CASE WHEN F.BRAND IS NULL THEN '' ELSE F.BRAND END) BRAND,
F.COLOR_NAME,
F.COLOR_TEMPLE,
F.[FRAME_TEMPLE],
F.FRAME_EYE_SIZE,
F.[FRAME_DBL],
F.[COLLECTION] ,
F.MODEL_NO ,
[DESCRIPTION],
IP.RETAIL_PRICE ,
IP.WHOLE_SALE AS WHOLE_SALE_PRICE
FROM
PMS_INVENTORY_ITEM_DETAILS I
INNER JOIN
PMS_INVENTORY_ITEM_PRICING_DETAILS IP ON I.INVENTORY_ITEM_ID = IP.INVENTORY_ITEM_ID
INNER JOIN
PMS_FRAME_SPECIFICATIONS F ON I.INVENTORY_ITEM_ID = F.INVENTORY_ITEM_ID
LEFT JOIN
PRACTICE_LOCATIONS PL ON PL.PRACTICE_LOCATION_ID = IP.LOCATION_ID
LEFT JOIN
[PMS_PURCHASE_ORDER_ITEMS] P ON P.UPC_ID = I.UPC_ID -- MBT#21084 Added by Trupti
LEFT JOIN
[PMS_PURCHASE_ORDERS] PO ON PO. PURCHASE_ORDER_ID = P.PURCHASE_ORDER_ID
WHERE
(IP.LOCATION_ID IN (3) OR #LOCATION_ID IS NULL)
--AND (I.UPC_ID IN (SELECT DISTINCT INVENTORY_ITEM_ID FROM #TABLE_INVENTORY_ITEM_ID ) OR #INVENTORY_ITEM_ID IS NULL)
AND ((I.UPC_ID IN ('01-4001') OR #INVENTORY_ITEM_ID IS NULL)
OR (PO.PO_NO IN (SELECT CAST(DATA AS INTEGER)
FROM dbo.SPLIT(#INVENTORY_ITEM_ID, ',')) OR #INVENTORY_ITEM_ID IS NULL))
AND (I.INVENTORY_CATEGORY = 'FRAMES')
AND (IP.IS_ACTIVE = 1)
AND (I.IS_ACTIVE = 1)
--ORDER BY IP.LAST_SOLD_DATE
--OFFSET (#PAGE_NO - 1) * #PAGE_SIZE ROWS FETCH NEXT #PAGE_SIZE ROWS ONLY
END
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH
END
If I execute the above whole code then I the image below shows output with error
Conversion failed when converting the varchar value '01-4001' to data type int.
But if execute only select query then it works fine. with no error.
My datatype are Varchar(max). Inventory_Item_id is = '01-4001'
If I put inventory_item_id as '2189' then whole execution doesn't throw error.
Response error in network like:
""error":true,"message":"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index""
In table PMS_INVENTORY_ITEM_DETAILS description INVENTORY_ITEM_ID is int length 4 and UPC_ID is varchar length -1.
The error occurs here:
INSERT INTO #TABLE_INVENTORY_ITEM_ID
SELECT * FROM DBO.SPLIT(#INVENTORY_ITEM_ID, ',')
The separator you provide for your split function is , rather than - so the result will be 01-4001 again (rather than the expected 01 and 4001), and converting that to an integer then fails.
To fix, use - as separator.
Hi Thanks for your help.
Issue is resolved
Changes made at line
OR (PO.PO_NO IN (SELECT CAST(DATA AS varchar) FROM DBO.SPLIT(#INVENTORY_ITEM_ID, ',')) OR #INVENTORY_ITEM_ID IS NULL))
CAST(DATA AS INTEGER) updated with CAST(DATA AS varchar)
as parameter #INVENTORY_ITEM_ID is coming as '01-4001' [ - is there so error casting to int ]

SQL scalar valued function throwing "Invalid Object Name" error

My SQL scalar valued function is defined in the following code:
CREATE FUNCTION CtrAmount ( #Ctr_Id int )
RETURNS MONEY
AS
BEGIN
DECLARE #CtrPrice MONEY
SELECT #CtrPrice = SUM(amount)
FROM Contracts
WHERE contract_id = #Ctr_Id
RETURN(#CtrPrice)
END
GO
SELECT * FROM CtrAmount(345)
GO
But when it comes to the SELECT line, I am getting this error:
Msg 208, Level 16, State 3, Line 14
Invalid object name 'CtrAmount'.
Int(10) - unknown type
CREATE FUNCTION dbo.CtrAmount
(
#Ctr_Id INT
)
RETURNS MONEY
AS
BEGIN
RETURN (
SELECT SUM(amount)
FROM dbo.Contracts
WHERE contract_id = #Ctr_Id
)
END
GO
SELECT dbo.CtrAmount(345)
GO

Disallowed implicit conversion from data type datetime to data type float, table 'TABLE', column 'FECHA'. Use the CONVERT function to run this query

I have this error, and I can't fix it
Msg 260, Level 16, State 3, Procedure SP_SAV_ESTADISTICALLAMADAXOPERADOR, Line 7
Disallowed implicit conversion from data type datetime to data type float, table 'APEX.SAV_LLAMADAS', column 'FECHA'. Use the CONVERT function to run this query.
this's my code
CREATE PROCEDURE APEX.SP_SAV_ESTADISTICALLAMADAXOPERADOR
#pCodOperador VARCHAR (20)
AS
BEGIN
SET NOCOUNT ON
SELECT TP.DESCRIPCION AS TIPOLOGIA,
ST.DESCRIPCION AS SUB_TIPOLOGIA,
COUNT(*) AS TOTAL
FROM APEX.SAV_LLAMADAS_DET DT INNER JOIN APEX.SAV_LLAMADAS LL
ON LL.ID_LLAMADA = DT.ID_LLAMADA INNER JOIN APEX.SAV_SUB_TIPOLOGIAS ST
ON DT.COD_TIPOLOGIA = DT.COD_TIPOLOGIA
AND DT.COD_SUBTIPOLOGIA = ST.COD_SUB_TIPOLOGIA INNER JOIN APEX.SAV_TIPOLOGIAS TP
ON TP.COD_TIPOLOGIA = ST.COD_TIPOLOGIA
WHERE DT.USUARIO_CREA = #pCodOperador
AND FLOOR(LL.FECHA) = FLOOR((GETDATE()))
GROUP BY TP.DESCRIPCION, ST.DESCRIPCION
SET NOCOUNT OFF
END
You can't do FLOOR on a datetime datatype. Assuming you are trying to exclude the time portion of a datetime you can do something like this instead.
AND CAST(LL.FECHA AS DATE) = CAST(GETDATE() AS DATE)

Working T-SQL query gives error when values assigned to variables

The following query works. However, when I uncomment the commented lines in order to assign query variables to my locally-defined variables, I get a syntax error at the final ";".
DECLARE #tmp_total_ct int = 0;
DECLARE #tmp_closed_ct int = 0;
--SELECT #tmp_total_ct = tot_ct, #tmp_closed_ct = cls_ct
-- FROM (
SELECT COUNT(*) AS tot_ct,
SUM(IIF(is_closed = 1, 1, 0)) AS cls_ct
FROM corp.CashDealDetails
INNER JOIN WidgeStores wstrs
ON corp.CashDealDetails.abbrev = wstrs.abbrev
WHERE mall_id IN
(SELECT mall_ndx FROM WidgeStores wstrs
WHERE wstrs.abbrev IN
(SELECT abbrev FROM #tmpMkts)
AND wstrs.[is_opn] = 1
)
AND YEAR(closing_dt) = 2012
--);
I believe that I'm using the correct syntax for assigning query values to user-defined variables, as explained in other posts on these threads. Where exactly is the syntax error?
declare #tmp_total_ct int
declare #tmp_closed_ct int
SELECT #tmp_total_ct = tot_ct, #tmp_closed_ct = cls_ct
FROM (
SELECT COUNT(*) AS tot_ct,
SUM(IIF(is_closed = 1, 1, 0)) AS cls_ct
FROM corp.CashDealDetails
INNER JOIN WidgeStores wstrs
ON corp.CashDealDetails.abbrev = wstrs.abbrev
WHERE mall_id IN
(SELECT mall_ndx FROM WidgeStores wstrs
WHERE wstrs.abbrev IN
(SELECT abbrev FROM #tmpMkts)
AND wstrs.[is_opn] = 1
)
AND YEAR(closing_dt) = 2012
) a
print #tmp_total_ct
print #tmp_closed_ct

Resources