2
votes

Insérer un espace après 3 caractères (SQL Express)

J'exécute SQL Server Express J'ai une colonne avec des codes postaux au format 12345 et je dois ajouter un espace après le troisième numéro comme 123 45


0 commentaires

4 Réponses :


3
votes

Il suffit d'utiliser la fonction FORMAT ()

SELECT format(5, '000 00')

Cela donnera 123 45

SELECT format(12345,'### ##') AS ZIP


0 commentaires

-1
votes

Quelques options:

DECLARE @Val VARCHAR(10) = '12345'

SELECT  @Val
,       STUFF(@Val, 4, 0, ' ')
,       CONCAT(LEFT(@Val, 3), ' ', RIGHT(@Val, 2))
,       FORMAT(CONVERT(INT, @Val),'### ##')


0 commentaires

1
votes

Cela le fera

CREATE TABLE #TBL (Zipcode varchar(50))
INSERT INTO #TBL VALUES ('12345')

SELECT 
Zipcode,
LEFT(Zipcode,3)+' '+RIGHT(Zipcode,LEN(Zipcode)-LEN(LEFT(Zipcode,3))) AS NewzipCode

FROM #TBL;

DROP TABLE #TBL


0 commentaires

0
votes

Cela a fonctionné.

select *
    ,CASE 
        when dbo.test.zip LIKE '[0-9][0-9][0-9][0-9][0-9]' then LEFT(dbo.test.zip,3) + ' ' + RIGHT(dbo.test.zip,2) 
        ELSE dbo.test.zip
        END
from test.zip


0 commentaires