简体   繁体   English

附加 varchar 字符串后将填充空格添加回 CHAR

[英]Add the padding spaces back to CHAR after appending varchar string

I have a CHAR(250) field , which , for example will initially contain 10 byte data and 240 bytes of space, so remaining it has 250-10=240 bytes .我有一个 CHAR(250) 字段,例如,它最初将包含 10 个字节的数据和 240 个字节的空间,所以剩下的它有 250-10=240 个字节。 Now I wanted to append a varchar of length say 20 bytes .现在我想附加一个长度为 20 bytes 的 varchar 。 So it will go to 240 + 20 =260 bytes which while inserting back to CHAR field will give size overflow issue.所以它将变为 240 + 20 =260 字节,在插入回 CHAR 字段时会出现大小溢出问题。

Can do this , but I need the actual length , that means i want to re-pad the final string , ie 250 bytes in the final length ,可以这样做,但我需要实际长度,这意味着我想重新填充最终字符串,即最终长度为 250 个字节,

DECLARE @Var CHAR(250)='var lenght initial Data'
SELECT replace(@Var,' ','')
DECLARE @TempVar VARCHAR(20) = 'new text to be added'
SET @FinalVar=@Var + @TempVar
--Need to add final padding back to make it back to 250 bytes

Is there any other way rather counting and re-adding the space pads back ?有没有其他方法可以计算并重新添加空格键?

This is really your expected result?这真的是你预期的结果吗?

The initial value all in the left, and the new values beeing added to the right?初始值都在左边,新值加到右边?

在此处输入图像描述

As you never tagged the DB that you have (as the sql tag info says to do), so I'm assuming MySQL/MariaDB here...由于您从未标记过您拥有的数据库(正如sql标记信息所说的那样),所以我在这里假设 MySQL/MariaDB ...

The replace is not really doing anything, but it seems that you want: replace并没有真正做任何事情,但您似乎想要:
(and yes, no need to recount, by specifying a length) (是的,不需要重新计算,通过指定长度)

DECLARE Var CHAR(250) default 'var length initial Data';
-- SELECT replace(@Var,' ',''); -- ... is not saved anywhere, so I removed.
DECLARE FinalVar CHAR(250);
SELECT CONCAT(Var, ' ', otherText) INTO FinalVar;

So, putting that into a function:所以,把它放到一个函数中:

DELIMITER //

CREATE FUNCTION right_pad_spaces(otherText varchar(250)) RETURNS CHAR(250)
DETERMINISTIC
BEGIN
    DECLARE Var CHAR(250) default 'var length initial Data';
    DECLARE FinalVar CHAR(250);
    SELECT CONCAT(Var, ' ', otherText) INTO FinalVar;
    RETURN FinalVar ;
END; //

DELIMITER ;

Then use the function, RPAD the result with spaces, limited to 250 total length:然后使用函数,RPAD 带空格的结果,总长度限制为 250:

SELECT RPAD(right_pad_spaces('new text to be added'), 250, ' ') AS 'new string';

Output: (note the horizontal scroll bar)输出:(注意水平滚动条)

在此处输入图像描述

Checking the length of the same call:检查同一呼叫的长度:

SELECT Length(
    RPAD(right_pad_spaces('new text to be added'), 250, ' ')
) AS 'total length';

Gives:给出:

在此处输入图像描述

Here's a dbfiddle with that handling.这是一个处理该处理的dbfiddle
Know that the site does not use DELIMITER , so that was added for your local use.知道该站点不使用DELIMITER ,因此添加了它以供您本地使用。

So looks like there is no 'proper' fix to this but this is what I ended up with所以看起来没有“适当”的解决方法,但这就是我最终得到的

DECLARE @Var CHAR(250)='var lenght initial Data'
SELECT @Var=replace(@Var,' ','')
DECLARE @TempVar VARCHAR(20) = 'new text to be added'
SET @FinalVar=@Var + @TempVar
SELECT @TempEmails= CAST(left(@FinalVar, 250) AS CHAR(250)) --CAST to char pads back to 250 , this is the easiest way

the Tweak is the CAST AS CHAR(250) which will re-pad the string to 250 Tweak 是CAST AS CHAR(250)它将重新填充字符串到 250

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM