mercoledì 27 agosto 2008

Extract only numbers from a String in SQL Server

Circa una settimana fa, davanti ad un buon caffè, Guglielmo ovviamente :-), un mio carissimo amico mi chiese una Function in SQL Server che estraesse da una stringa solo numeri.
Lavorando da molti anni in Oracle ho subito pensato alle functions Translate or regexp_replace (10g).
Ma poi mi sono ricordato che in SQL Server, la funzione Translate non esiste e l’utilizzo delle “Regular Expression” è possibile sono by CLR .

Allora documentandomi un po’ in giro ecco una soluzione carina, spero!?! :-)

declare @in_string varchar(255)
declare @out_string varchar(255)

set @in_string='Carlopoli21082008Claudio&Rino&pureSergio'
set @out_string = ''


select @out_string = @out_string + a.sub_char
from
(

select substring(@in_string, b.number,1) as sub_char
from master..spt_values b
where b.type = 'p'
and b.number between 1 and len(@in_string)
) as a
where isnumeric(a.sub_char) = 1


print @out_string

Ciao.
PS: Ringrazio il "vecchio", ma sempre saggio, Arcuri ... :-)

2 commenti:

LOVELY RIMA ha detto...

Our Delhi Escort Girl is independent escort model like me in your city then i am available here for you. Hi I am a model , I am young smart and well educated Independent Girl Available for fulfill all your needs.

283kwedpzf ha detto...

seven-hundred casino grade colour wheel checks and Casino Royal impressed worth chips. Bring the casino home with the James Bond Collector’s Edition Roulette 메리트카지노 Wheel from world-leading roulette wheel makers, Cammegh. There are particular Roulette chips if you sit method down to} play. To keep away from disputes, every Guest receives a unique color chip; these chips are good for that desk only and have to be cashed in when you are up} carried out half in}. The object of Roulette is to choose the profitable number that will seem on the Roulette wheel. You can even choose combinations of numbers, the color or whether it's odd or even.