我是 SQL 人,是 Spark SQL 的新手
我需要找到字符索引 '-' 在字符串中的位置,如果有那么我需要输入字符的固定长度,否则长度为零
string name = 'john-smith'
如果“-”在字符位置 4 则为 10 否则长度为 0
我已经在 SQL Server 中完成,但现在需要在 Spark SQL 中完成。
select
case
when charindex('-', name) = 4 then 10
else 0
end
我在 Spark SQL 中尝试过,但没有得到结果。
select find_in_set('-',name)
请帮忙。谢谢
最佳答案
您可以使用 instr 函数,如下所示。 insrt 检查第二个 str 参数是否是第一个参数的一部分,如果是,则返回从 1 开始的索引。
//first create a temporary view if you don't have one already
df.createOrReplaceTempView("temp_table")
//then use instr to check if the name contains the - char
spark.sql("select if(instr(name, '-') = 4, 10, 0) from temp_table")
if 语句的参数是:
instr(name, '-') = 4
检查条件关于apache-spark-sql - 在spark sql中查找字符串中的字符索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56848721/