在 spark sql 中查找字符串中的字符索引
Find a character index in string in spark sql
我是 SQL 人,刚接触 Spark SQL
我需要找到字符索引'-'在字符串中的位置,如果有则我需要输入字符的固定长度,否则长度为零
string name = 'john-smith'
如果 '-' 在字符位置 4 则为 10 否则长度为 0
我已经在 SQL 服务器中完成,但现在需要在 Spark SQL 中完成。
select
case
when charindex('-', name) = 4 then 10
else 0
end
我在 Spark SQL 中尝试过,但没有得到结果。
select find_in_set('-',name)
请帮忙。谢谢
您可以使用如下所示的 instr 函数。 insrt 检查第二个 str 参数是否是第一个参数的一部分,如果是,它 returns 它的索引从 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
要检查的条件
- 10 个有效条件的结果
- 错误条件的 0 个结果
我是 SQL 人,刚接触 Spark SQL
我需要找到字符索引'-'在字符串中的位置,如果有则我需要输入字符的固定长度,否则长度为零
string name = 'john-smith'
如果 '-' 在字符位置 4 则为 10 否则长度为 0
我已经在 SQL 服务器中完成,但现在需要在 Spark SQL 中完成。
select
case
when charindex('-', name) = 4 then 10
else 0
end
我在 Spark SQL 中尝试过,但没有得到结果。
select find_in_set('-',name)
请帮忙。谢谢
您可以使用如下所示的 instr 函数。 insrt 检查第二个 str 参数是否是第一个参数的一部分,如果是,它 returns 它的索引从 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
要检查的条件- 10 个有效条件的结果
- 错误条件的 0 个结果