都内で働くSEの技術的なひとりごと / Technical soliloquy of System Engineer working in Tokyo

都内でサラリーマンやってます。SQL Server を中心とした (2023年からは Azure も。) マイクロソフト系(たまに、OSS系などマイクロソフト以外の技術も...)の技術的なことについて書いています。日々の仕事の中で、気になったことを技術要素関係なく気まぐれに選んでいるので記事内容は開発言語、インフラ等ばらばらです。なお、当ブログで発信、発言は私個人のものであり、所属する組織、企業、団体等とは何のかかわりもございません。ブログの内容もきちんと検証して使用してください。英語の勉強のため、英語の

Some useful SQL functions, syntax, and more - Part 18 ( REIWA GANNEN Memorial Post )

The end of HEISEI ( 平成 ), REIWA ( 令和 ) has begun. The last year of HEISEI was turning point in my life. However, I still want to evolve, so I want to move forward without fear of change.
I will explain about TRANSLATE (Transact-SQL) - SQL Server | Microsoft Docs. Complex processing in REPLACE function can be described very simply by TRANSLATE function. Let's compare the description contents of REPLACE function and TRANSLATE function.

Let's execute the following query in SQL Server Management Studio.

DECLARE @str NVARCHAR(255) = 'aaa,aaa,aaa|bbb,bbb,bbb'

SELECT
    REPLACE(REPLACE(REPLACE(@str,'|','*'),',','|'),'*',',') [REPLACE],
    TRANSLATE(@str, ',|', '|,') [TRANSLATE]

f:id:koogucc11:20190429083951p:plain
Execution result of REPLACE and TRANSLATE

The features of TRANSLATE function are as follows.

The behavior of the TRANSLATE function is similar to using multiple REPLACE functions. TRANSLATE does not, however, replace a character more than once. This is dissimilar to multiple REPLACE functions, as each use would replace all relevant characters.

In stored procedures, I often find that the RELACE function is nested in multiple layers. Although not available in all cases, use TRANSLATE function to refactor complex stored procedures.

I bought this magazine. I will do my best to study English!!