LPAD en SQL Server 2008


No puedo ver una función como LPAD en SQL Server 2008. Por ejemplo, ¿cómo puedo convertir las siguientes consultas en T-SQL? Gracias por sus respuestas

select  LPAD(MY_VALUE,2,' ')) VALUE
FROM MY_TABLE
Author: Jon Heller, 2011-05-06

4 answers

Básicamente lo rellena con el número de caracteres que deseas seleccionar y luego endereza la cadena.

Select right(replicate(' ',2) + YourFieldValue,2) from YourTable

Puede usar la función space en lugar de replicate, space(number_of_spaces), replicate solo le permite rellenar con caracteres alternativos.

 31
Author: Andrew,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-06 14:00:34

Los cálculos manuales pueden ser molestos para aplicar consultas internas. Afortunadamente, podemos crear una función:

CREATE FUNCTION LPAD
(
    @string VARCHAR(MAX), -- Initial string
    @length INT,          -- Size of final string
    @pad CHAR             -- Pad character
)
RETURNS VARCHAR(MAX)
AS
BEGIN
    RETURN REPLICATE(@pad, @length - LEN(@string)) + @string;
END
GO

(Por favor reemplace VARCHAR con NVARCHAR a su gusto, o use un algoritmo alternativo.)

Entonces:

SELECT dbo.LPAD(MY_VALUE, 2, ' ') VALUE
FROM MY_TABLE

Debería funcionar desde SQL Server 2005.

 4
Author: Álvaro González,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-30 18:48:03

Necesitaba algo similar pero no podía usar '+' porque entonces se interpretó como una suma entre los números '0000000000' y [seq_no]. Así que usé concat en su lugar y funcionó bien.

select right (concat(replicate('0', 10),  next value for seq_no), 10);
 2
Author: Jonathan Edwards,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-16 04:32:32

Se me ocurrió una función LPAD y RPAD donde puedes usar una cadena de caracteres para la parte pad.

Deberían funcionar igual que las versiones DB2.

Aquí está el LPAD:

CREATE FUNCTION dbo.LPAD
(
    @string NVARCHAR(MAX), -- Initial string
    @length INT,           -- Size of final string
    @pad NVARCHAR(MAX)     -- Pad string
)
RETURNS VARCHAR(MAX)
AS
BEGIN
    RETURN SUBSTRING(REPLICATE(@pad, @length),1,@length - LEN(@string)) + @string;
END
GO

Y aquí está el RPAD:

CREATE FUNCTION dbo.RPAD
(
    @string NVARCHAR(MAX), -- Initial string
    @length INT,           -- Size of final string
    @pad NVARCHAR(MAX)     -- Pad string
)
RETURNS VARCHAR(MAX)
AS
BEGIN
    RETURN @string + SUBSTRING(REPLICATE(@pad, @length),1,@length - LEN(@string));
END
GO
 1
Author: Jeroen van der Molen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-10 11:04:58