cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

SQL charindex function?

Barb
New Contributor III

Hi all,

I need to use the SQL charindex function, but I'm getting a databricks error that this doesn't exist. That can't be true, right?

Thanks for any ideas about how to make this work!

Barb

6 REPLIES 6

shyam_9
Valued Contributor
Valued Contributor

Hi @Barb Krienke, Please use %sql in Databricks notebook cell.

%sql
SELECT CHARINDEX('mer', 'Customer', 3) AS MatchPosition;

Barb
New Contributor III

Hi @Shyamprasad Miryala​ 

I added %sql to the notebook cell and it did not work.

Were you able to get it to work in Databricks?

jconno
New Contributor II

%sql

--The closest thing I found was instr()

SELECT instr(Attribute, 'MatchString')

FROM TableA tba

Barb
New Contributor III

Thanks for trying @jconno​ 

Unfortunately I need to be able to specify the position where the search for the character needs to start. So just finding the first occurrence of the character is not going to help.

RAJUMASARAPU
New Contributor II

Hi @bkrienke,

It may usefull.

%sql

SELECT position('bar', 'foobarbar', 5);

-- 7

Traveller
New Contributor II

The best option I found to replace CHARINDEX was LOCATE, examples from the Spark documentation below

> SELECT locate('bar', 'foobarbar', 5);

7

> SELECT POSITION('bar' IN 'foobarbar');

4

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.