2017-10-04 1 views
0

Quelqu'un peut-il aider s'il vous plaît avec une fonction définie par l'utilisateur SQL Server 2008 pour ajouter des espaces entre une chaîne ou un nombre?Fonction définie par l'utilisateur SQL Server 2008 pour ajouter des espaces entre chaque chiffre

Ex: pour convertir un nombre 12345-1 2 3 4 5

+2

Tout ce que vous avez essayé? – Prisoner

+1

Google simple vous dirigera vers ce lien qui fait exactement ce que vous voulez. https://www.codeproject.com/Tips/426728/T-SQL-Function-Add-a-space-between-all-characters –

+0

Merci, cela m'a aidé !! – AHS

Répondre

0

Avec ngrams8k vous pouvez créer ceci:

create function dbo.itvf_padtext(@string varchar(8000)) 
returns table with schemabinding as return 
select newString = 
stuff((select ' '+token 
from dbo.ngrams8k(@string,1) 
order by position 
for xml path('')),1,1,''); 

Pour utiliser

select newString from dbo.itvf_padtext('558899'); 

retours

5 5 8 8 9 9 

contre une table

declare @sometable table (someid int identity, somestring varchar(100)); 
insert @sometable(somestring) values ('abc'), ('567'), ('pdqxxx'); 

select someString, newString 
from @sometable t 
cross apply dbo.itvf_padtext(t.somestring); 

retours

someString  newString 
--------------- ------------ 
abc    a b c   
567    5 6 7   
pdqxxx   p d q x x x  

Mise à jour - montrant pourquoi vous ne voulez pas utiliser un udf scalaire (tel que recommandé dans les commentaires OP

Voici un test de performance comparant l'udf scalaire à l'iTVF; notez mes commentaires dans le code. Ce que j'ai posté sera posté est 2-5 fois plus rapide.

-- sample data 
if object_id('tempdb..#strings') is not null drop table #strings; 
select top (10000) -- 10 rows, 5 to 16 characters 
    string = left(cast(newid() as varchar(36)), abs(checksum(newid())%17)+5) 
into #strings 
from sys.all_columns a, sys.all_columns b; 
go 
-- note that the scalar udf will only run with a serial execution plan 
print 'scalar'+char(13)+char(10)+replicate('-',50) 
go 
    declare @st datetime = getdate(), @x varchar(36); 
    select @x = dbo.udf_PutSpacesBetweenChars(t.string) 
    from #strings t; 
print datediff(ms,@st,getdate()) 
go 3 

print 'ngrams serial'+char(13)+char(10)+replicate('-',50) 
go 
    declare @st datetime = getdate(), @x varchar(36); 
    select @x = newstring 
    from #strings t 
    cross apply dbo.itvf_padtext(t.string) 
    option (maxdop 1); --force a serial plan 
print datediff(ms,@st,getdate()); 
go 3 

print 'ngrams parallel'+char(13)+char(10)+replicate('-',50) 
go 
    declare @st datetime = getdate(), @x varchar(36); 
    select @x = newstring 
    from #strings t 
    cross apply dbo.itvf_padtext(t.string) 
    option (recompile, querytraceon 8649); -- force a parallel plan 
print datediff(ms,@st,getdate()) 
go 3 

Résultats

scalar 
-------------------------------------------------- 
Beginning execution loop 
116 
114 
120 
Batch execution completed 3 times. 

ngrams serial 
-------------------------------------------------- 
Beginning execution loop 
50 
50 
50 
Batch execution completed 3 times. 

ngrams parallel 
-------------------------------------------------- 
Beginning execution loop 
23 
24 
20 
Batch execution completed 3 times.