I have a string called Dats
which is either of the general appearence xxxx-nnnnn
(where x
is a character, and n
is a number) or nnn-nnnnnn
.
I want to return only the numbers.
For this I've tried:
SELECT Distinct dats,
Left(SubString(artikelnr, PatIndex('%[0-9.-]%', artikelnr), 8000), PatIndex('%[^0-9.-]%', SubString(artikelnr, PatIndex('%[0-9.-]%', artikelnr), 8000) + 'X')-1)
FROM ThatDatabase
It is almost what I want. It removes the regular characters x
, but it does not remove the unicode character -
. How can I remove this as well? And also, it seems rather ineffective to have two PatIndex
functions for every row, is there a way to avoid this? (This will be used on a big database where the result of this Query will be used as keys).
EDIT: Updated as a new database sometimes contained additional -
's or .
together with -
.
DECLARE @T as table
(
dats nvarchar(10)
)
INSERT INTO @T VALUES
('111BWA30'),
('115-200-11')
('115-22.4-1')
('10.000.22')
('600F-FFF200')