vokial-the-death-knight / sql_str_replace

str_replace in sql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sql str_replace

fixed php str_replace() as sql function

Examples:

1.

select str_replace('a,b', 'b,c', 'abca');

output: 'bccb'

select str_replace("%body%", "black", "<body text='%body%'>");

output: '<body text=\'black\'>'

select str_replace(
  CONCAT_WS(",", "fruits", "vegetables", "fiber"), 
  CONCAT_WS(",", "pizza", "beer", "ice cream"), 
  "You should eat fruits, vegetables, and fiber every day."
);

or

select str_replace(
  "fruits,vegetables,fiber", 
  "pizza,beer,ice cream", 
  "You should eat fruits, vegetables, and fiber every day."
);

output: 'You should eat pizza, beer, and ice cream every day.'

Such PHP (example of potential str_replace() gotchas) code

<?php
$search  = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

will end up producing output 'F', because as the docs says:

Outputs F because A is replaced with B, then B is replaced with C, and so on... Finally E is replaced with F, because of left to right replacements.

With sql str_replace such weird behaviour will not happen

select str_replace(
  CONCAT_WS(',', 'A', 'B', 'C', 'D', 'E'),
  CONCAT_WS(',', 'B', 'C', 'D', 'E', 'F'),
  'A' 
);

output: 'B'


How does it work?

select str_replace('a,b', 'b,c', 'abca');

step by step:

  1. generate_safe_tokenizing_character:
output: `0.45348525`
  1. tokenize:
output: `'<0.4534852510.45348525><0.4534852520.45348525>c<0.4534852510.45348525>'`
  1. replace_tokenized:
output: `'bccb'`

final output: 'bccb'

About

str_replace in sql


Languages

Language:SQLPL 100.0%