Editing Columns with trdsql
So far, we have rearranged the columns, but the content of the columns has remained the same. Although SQL is not known for its ability to rewrite strings, it does have a decent set of features that can be used by using SQL functions.
Concatenating Columns
By using ‘||’, if you connect the column names, two or more columns will be concatenated into one column.
PostgreSQL and MySQL
In PostgreSQL and MySQL, you can use concat(column name or string, column name or string, …) when you want to concatenate multiple columns.
If you want to concatenate with a connection character, you can use the concat_ws function. The concat_ws function is a function that concatenates columns with a connection character.
SQLite3
SQLite3 does not have a concat function like PostgreSQL and MySQL. However, you can concatenate columns using the ‘||’ operator, as shown in the first example.
If you want to concatenate with a connection character, you can use the printf function. The printf function is a function that formats strings in the same way as the C language’s printf function.
Substring
Use only a part of the column string. Use the substr(column name or string, start position, length of characters) function.
Replace Characters
For example, replace ’e’ in the name column with ‘x’.
The number of characters before and after replacement does not have to be the same.
There are also functions for converting to uppercase and lowercase.
All to lowercase
All to uppercase
If you can get by with this level of rewriting, it’s fine, but in scripting languages, it’s common to use regular expressions for rewriting, so it looks inferior. PostgreSQL and MySQL engines have a few more useful functions.
regexp_replace
Use PostgreSQL’s regexp_replace(column name or string, regular expression pattern, replacement string) to replace the second to fourth characters with x.
Patterns enclosed in () can be used as reference characters \1, \2, etc.
MySQL seems to be able to use regexp_replace() from 8.0. However, the reference characters are $1, $2, etc. And if you use $1 in the command arguments, it will be interpreted by the shell, so you need to escape it with .