ero alla ricerca script per estrarre il numero di testo in SQL server e ho trovato questonumeri estratto da un testo in SQL Server
CREATE FUNCTION [dbo].[GetNumbersFromText](@String VARCHAR(2000))
RETURNS @Number TABLE (Number INT)
AS
BEGIN
DECLARE @Count INT
DECLARE @IntNumbers VARCHAR(1000)
SET @Count = 0
SET @IntNumbers = ''
WHILE @Count <= LEN(@String)
BEGIN
--Find a numeric charactor
IF SUBSTRING(@String,@Count,1) >= '0' AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + SUBSTRING(@String,@Count,1)
END
--If the next charactor is not a numeric one, the current number ends, so add a separator
IF (SUBSTRING(@String,@Count+1,1) < '0'OR SUBSTRING(@String,@Count+1,1) > '9') AND SUBSTRING(@String,@Count,1) >= '0' AND SUBSTRING(@String,@Count,1) <= '9'
BEGIN
SET @IntNumbers = @IntNumbers + ','
END
SET @Count = @Count + 1
END
---Split string to give a table with the numbers in the text
INSERT INTO @Number
SELECT DISTINCT items FROM dbo.Split(@IntNumbers, ',')
return
END
e lo chiamo come
SELECT Number FROM Dbo.[GetNumbersFromText]('Give me 120 this week and 50 next week')
funziona benissimo ma ho bisogno di più codice corto posso usare patindex per estrarre il numero dal testo. per favore chiunque condivida la piccola & buona logica per farlo. grazie
Perché avete bisogno "codice più corto" e "piccola logica"? Se la tua funzione funziona correttamente e funziona in modo accettabile, non c'è alcun motivo per cambiarla. – Pondlife