![](/img/trans.png)
[英]Trimming both 0 and trailing/leading spaces doesn't give results - MySQL
[英]Remove trailing spaces and add them as leading spaces
我想從列中的表達式中刪除尾隨空格,並將其添加到表達式的開頭。 因此,例如,我目前有以下表達式:
Sample_four_space
Sample_two_space
Sample_one_space
我想將此列轉換為:
Sample_four_space
Sample_two_space
Sample_one_space
我已經嘗試過以下表達式:
UPDATE My_Table
SET name = REPLACE(name,'% ',' %')
但是,我想要一個更健壯的查詢,該查詢適用於任何長度的尾隨空格。 您能幫我開發一個查詢,以刪除所有尾隨空格並將其添加到表達式的開頭嗎?
如果您知道所有空格都在末尾(如您的示例中所示,那么您可以算出它們並將其放在開頭:
select concat(space(length(name) - length(replace(name, ' ', ''))),
replace(name, ' ', '')
)
否則,更好的解決方案是:
select concat(space( length(name) - length(trim(trailing ' ' from name)) ),
trim(trailing ' ' from name)
)
要么:
select concat(space( length(name) - length(rtrim(name)) ),
rtrim(name)
)
這兩種情況都計算空格數(在空格中或結尾)。 然后, space()
函數將復制空格並將concat()
放在開頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.