Solved

Replace transformation with char type requires looking up the int value of a char

  • 15 February 2023
  • 5 replies
  • 120 views

Userlevel 5
Badge +7

I discovered that when using the replace transformation with the CharType that you are required to know or look up the int value of the character you want to replace and use that as the parameter.

This does not seem to be documented anywhere or be useful in any way I can think of. I assume REPLACE('abc’, char(65), 'b’) is functionally equivalent to REPLACE('abc’, 'a’, 'b’). In a Case Insensitive collation there is no difference between the two anyhow.

There is also no COLLATION or binary type support, which are things that you can do with REPLACE() that might actually be useful in certain rare cases.

 

icon

Best answer by Christian Hauggaard 16 February 2023, 14:22

View original

5 replies

Userlevel 6
Badge +5

Hi Rory 

The chartype only supports numeric values as indicated by the warning message, if you try to use non-numeric values. 

Is there a particular reason why you can’t use the stringtype replace?

 

Userlevel 5
Badge +7

Hi Christian,

There is no specific reason but there also doesn't seem to be a specific reason for the char type to exist (other than a very specific subset of people who prefer ASCII codes over using letters directly). As the string type also lets you use substrings where the char type is single-character only, it is just strange for most people.

Userlevel 6
Badge +5

@rory.smith One example of where ASCII codes are useful is CHAR(2) - a none display character (either null or empty used in AX/Oracle) which usually needs to be replaced. Since you would not be able to just type in the value, the option to input ASCII codes is necessary to replace such a character.

Userlevel 5
Badge +7

Hi Christian,

 

couldn't you achieve that with a unicode codepoint in a string literal? That would be SELECT N'␂' in SSMS

Userlevel 6
Badge +5

@rory.smith perhaps.. please feel free to submit this as an idea :)

Reply