How to Implement oracle's substrb(str2||str1,1+lengthb(str1),6) in TiDB

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: oracle的substrb(str2||str1,1+lengthb(str1),6)如何在tidb中实现

| username: TiDBer_sclAjWFq

To improve efficiency, please provide the following information. A clear problem description can help resolve the issue faster:

[Overview] Scenario + Problem overview function modification

[Application Framework and Development Adaptation Business Logic]

[Background] Operations performed

[Phenomenon] Business and database phenomena

[Problem] Current issue encountered

[Business Impact]

[TiDB Version]

[Attachments] Relevant logs and monitoring

| username: Kongdom | Original post link

It should be the same, right? Are you specifically referring to the difference between substrb and substr?

| username: zhanggame1 | Original post link

SELECT SUBSTRING(CONCAT(str2, str1), LENGTH(str1) + 1, 6) FROM your_table;
| username: forever | Original post link

TiDB is compatible with MySQL, so you can rewrite it using MySQL functions.

| username: wangccsy | Original post link

I’ve never used it.

| username: tidb菜鸟一只 | Original post link

Isn’t this just converting Oracle to MySQL?

| username: linnana | Original post link

From the function name, it should be distinguished by the number of bytes and the number of characters.

| username: Kongdom | Original post link

:joy: It seems that no one is paying attention to the difference between substrb and substr!

| username: linnana | Original post link

substr(cast(concat(str2, str1) as binary), 1 + length(cast(str1 as binary)), 6)

| username: 小龙虾爱大龙虾 | Original post link

What is this recording, can you write SQL like this :joy_cat:

| username: forever | Original post link

One byte, one character :joy:

| username: Kongdom | Original post link

:+1: I only found out after searching on Baidu~ :yum:

| username: forever | Original post link

These basic functions are generally available in every database, and it’s very simple to look up and modify them according to your own needs. Some time ago, I worked with a DBA from Tencent to convert Oracle functions to TDSQL. We broke them down into a bunch of small stored procedures and made the changes.

| username: changpeng75 | Original post link

For single-byte encodings like English, substr and substrb are the same. However, when used with multi-byte characters like Chinese, substrb may result in garbled text. Since you are migrating from Oracle to TiDB, you might as well change it conveniently without insisting on it.

| username: ShawnYan | Original post link

Literally, it’s the difference between string and string byte?

| username: changpeng75 | Original post link

Bytes and characters.

| username: Kongdom | Original post link

The difference is between bytes and characters. I specifically looked it up on Baidu to find out~ I hadn’t paid attention to it before.

| username: 哈喽沃德 | Original post link

Writing it this way has no performance to speak of, unless your result set is very small.

| username: chris-zhang | Original post link

Never heard of it.

| username: wangkk2024 | Original post link

Refer to MySQL syntax.