Search code examples
sqlreplacedb2db2-zos

Replacing the 113th position in a string with 'X' in DB2 SQL


I am looking for a solution for replaceing the 113th position within a string.

As of now the only solution that I found was this one, however, it is not a solution because it replaces all spaces with X.

I cannot use the STUFF function or something like that because I am running on DB2 V11.2.

Here is what I have so far:

SELECT
  REPLACE(COLUMN_NAME, SUBSTR(COLUMN_NAME,113,1), 'N')
FROM TABLE_NAME;

The result is that all spaces are replaced with N. However, I just want the 113th position replaced with an 'N', and if the string is not that long, nothing should happen!

Since, as it seems, I have quite an old version of DB2, I have no CHARINDEX, STUFF, or whatsoever function available.

Maybe, someone has an idea how I can change just the character at position 113th?

Thank you very much in advance!


Solution

  • Please try this: overlay(column_name,'N', 113, 1)