March 6, 2014 at 7:23 am
ronmoses (3/6/2014)
Richard Warr (3/6/2014)
It's anything but logical behaviour and you can only get the question correct if you know that caveat on the REPLACE function (or if you just run the code).Developer A: "We have a bug in this function. Want me to fix it?"
Developer B: "Nah, just document it as normal behavior."
Developer A: "Done."
Developer B: Consider it a feature. 😀
Thanks for the great question.
March 6, 2014 at 7:44 am
Interesting question. 🙂
March 6, 2014 at 8:04 am
nice and easy.
thanks.
March 6, 2014 at 9:07 am
+1
March 6, 2014 at 11:56 am
Interesting, Thanks for the question.
March 8, 2014 at 6:11 am
Even though this is a simple one to remember, when strings are taken from a table ample are the chances that this behavior would be forgotten.
March 8, 2014 at 9:47 am
@Anoo
This is exactly what happened to me and how I found out about this behaviour.
March 8, 2014 at 2:35 pm
@auke - And together we learn by sharing our mistakes. Thank you for sharing the question.
April 3, 2014 at 7:27 am
auke.teeninga (3/6/2014)
In many programming languages the third argument wouldn't even be evalutated if the second argument didn't appear in the first argument.All these will just return 'ABC':
SELECT REPLACE('ABC', 'Z', 1)
SELECT REPLACE('ABC', 'Z', CURRENT_TIMESTAMP)
SELECT REPLACE('ABC', 'Z', '#$@%')
This one will return 'ABC' half of the time and NULL the other half.
SELECT REPLACE('ABC', 'Z', CASE WHEN RAND() < 0.5 THEN 'A' END)
I thought of making the problem more complex, but I figured that would result in more people just running the query instead of answering first! 😉
So, although the actual QotD is straightforward, one has to be careful to guard against having anything in the REPLACE statement that evaluates to NULL.
May 24, 2014 at 2:29 pm
ronmoses (3/6/2014)
Richard Warr (3/6/2014)
It's anything but logical behaviour and you can only get the question correct if you know that caveat on the REPLACE function (or if you just run the code).Developer A: "We have a bug in this function. Want me to fix it?"
Developer B: "Nah, just document it as normal behavior."
Developer A: "Done."
+1
Viewing 11 posts - 16 through 25 (of 25 total)
You must be logged in to reply to this topic. Login to reply