在 SQL 中将字符串转换为元组
Convert string to tuple in SQL
我正在使用 python 在 snowflake 中调用 SQL 脚本到 运行。 python 脚本将一个元组作为字符串传递给 SQL 的 where 子句,但是如何在 SQL 中将字符串转换回元组?
例如,SQL 查询如下所示:
where catalog_item_id in '(1180, 3190)'
而它应该是 where catalog_item_id in (1180, 3190)
.
我尝试了 where catalog_item_id in (select cast('(1180, 3190)' as varchar))
和 where catalog_item_id in (select replace('(1180, 3190)', '''', ''))
,其中 none 有效。有人可以帮忙吗?
提前致谢!
回复评论,我的代码在python:
file = open(fnm, 'r')
product_list = (1180, 3190)
query = file.read().replace('{item_ids}', str(product_list))
file.close()
sql 查询看起来像
select
catalog_item_id,
score
from ITEM_RECOMMENDATIONS
where catalog_item_id in '{item_ids}'
select catalog_item_id,score
from ITEM_RECOMMENDATIONS
where catalog_item_id in (select value from split_to_table(input_param, ','));
其中 input_param 应提供为:'1180,3190'
我正在使用 python 在 snowflake 中调用 SQL 脚本到 运行。 python 脚本将一个元组作为字符串传递给 SQL 的 where 子句,但是如何在 SQL 中将字符串转换回元组?
例如,SQL 查询如下所示:
where catalog_item_id in '(1180, 3190)'
而它应该是 where catalog_item_id in (1180, 3190)
.
我尝试了 where catalog_item_id in (select cast('(1180, 3190)' as varchar))
和 where catalog_item_id in (select replace('(1180, 3190)', '''', ''))
,其中 none 有效。有人可以帮忙吗?
提前致谢!
回复评论,我的代码在python:
file = open(fnm, 'r')
product_list = (1180, 3190)
query = file.read().replace('{item_ids}', str(product_list))
file.close()
sql 查询看起来像
select
catalog_item_id,
score
from ITEM_RECOMMENDATIONS
where catalog_item_id in '{item_ids}'
select catalog_item_id,score
from ITEM_RECOMMENDATIONS
where catalog_item_id in (select value from split_to_table(input_param, ','));
其中 input_param 应提供为:'1180,3190'