Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maykonn/415dd48a6be57594426b6af95f4b205b to your computer and use it in GitHub Desktop.
Save Maykonn/415dd48a6be57594426b6af95f4b205b to your computer and use it in GitHub Desktop.
SQL search by specific string in a comma separated string (Solution for Redshift too).

Will return 0 or false:

SELECT "5,18,5,7,9,2" REGEXP "[[:<:]](8)[[:>:]]"

Will return 1 or true:

SELECT "5,18,5,7,9,2" REGEXP "[[:<:]](5)[[:>:]]"

For Redshift you must use ~ instead REGEXP on above statements:

SELECT "5,18,5,7,9,2" ~ "[[:<:]](5)[[:>:]]"

MySQL have a a function called find_in_set():

SELECT FIND_IN_SET("c", "a,b,c,d,e");
@vprasadr
Copy link

Any options in redshift to compare string with values in comma separated between two columns.
like col1 = '1,2,3' and col2 '1,2,4,5' for this case the output should give 1 since 1,2 are present in col2
select 1 from tab where col1 like col2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment