New built-in function TRANSLATE() in SQL Server 2017
Microsoft looks very serious this time to move people from other databases to SQL Server. As with SQL Server 2016 & 2017 you can see lot of Built-in function added, which were present in other databases from long back, will ease database development in SQL Server.
One of this function is TRANSLATE() function, which can be used like a REPLACE() function, and would avoid using REPLACE() function multiple times in a query.
Syntax:
TRANSLATE ( inputString, characters, translations)
Note: characters and translations params should have same length.
–> Consider this example I’ve taken from MSDN:
SELECT TRANSLATE('2*[3+4]/{7-2}', '[]{}', '()()'); GO
Output:
Input | Output |
2*[3+4]/{7-2} | 2*(3+4)/(7-2) |
–> If you had to do same with REPLACE() function then you would end up writing multiple & nested REPLACE() function, like:
SELECT REPLACE( REPLACE( REPLACE( REPLACE('2*[3+4]/{7-2}', '[', '('), ']', ')'), '{', '('), '}', ')'); GO
After working with this new feature it reminds me of IIF vs CASE statement. The IIF() function also works as a shortcut of CASE statement and cuts lot of clutter and gives you clean code.
Hope you find this small utility very handy while developing complex queries, will post more scenarios if I came across going forward, thanks !!!
Reblogged this on .