Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在3个字符后插入空格(SQL Express)_Sql_Sql Server Express - Fatal编程技术网

在3个字符后插入空格(SQL Express)

在3个字符后插入空格(SQL Express),sql,sql-server-express,Sql,Sql Server Express,我正在运行SQLServerExpress 我有一个邮政编码为12345格式的专栏 我需要在第三个数字后面加一个空格,比如123 45 SELECT format(12345,'### ##') AS ZIP 将导致123 45 SELECT format(5, '000 00') 将导致00005 另外,如果希望0作为占位符,请在格式化字符串中将#替换为0 DECLARE @Val VARCHAR(10) = '12345' SELECT @Val , STUFF(@Val

我正在运行SQLServerExpress 我有一个邮政编码为12345格式的专栏 我需要在第三个数字后面加一个空格,比如123 45

SELECT format(12345,'### ##') AS ZIP
将导致
123 45

SELECT format(5, '000 00')
将导致
00005

另外,如果希望0作为占位符,请在格式化字符串中将#替换为0

DECLARE @Val VARCHAR(10) = '12345'

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

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
这起作用了

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