我在nvarchar中有以下值
0011223344
此值始终为均匀长度。
我需要将此值转换为00 \ 11 \ 22 \ 33 \ 44
使用MSSQL。
答案 0 :(得分:2)
这适用于SQL Server ..
DECLARE @test nvarchar(20) = '0011223344'
DECLARE @i int = 3
WHILE @i < LEN(@test)
BEGIN
SELECT @test = STUFF(@test, @i, 0, '\')
SET @i = @i + 3
END
SELECT @test
您可以使用数字表实现更优雅的解决方案。
答案 1 :(得分:1)
create function changeFormat( @BeginWord varchar(10)) returns varchar(20)
as
begin
declare @finalWord varchar(20)
SET @finalWord='';
SET @finalWord= @finalWord + substring(@BeginWord,1,2)+ '/';
SET @finalWord= @finalWord + substring(@BeginWord,3,2)+ '/';
SET @finalWord= @finalWord + substring(@BeginWord,5,2)+ '/';
SET @finalWord= @finalWord + substring(@BeginWord,7,2);
return @finalWord
end;
//调用函数
select word, dbo.changeFormat(word) as Formateado from table1;
word Formateado
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
11223344 11/22/33/44
答案 2 :(得分:-1)
DECLARE @Var NVARCHAR(10) = '0011223344'
SELECT LEFT(@Var, 2) + '\' + SUBSTRING(@Var, 3, 2)+ '\' + SUBSTRING(@Var, 5, 2)
+ '\' + SUBSTRING(@Var, 7, 2)+ '\' + SUBSTRING(@Var, 9, 2)
<强>结果强>
00\11\22\33\44