Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL Server can you find the first number in a string?

I have a varchar(50) sql server column with data like this:

RawData
----------------------------
Washington 40 New Orleans 32
Detroit 27 St. Louis 23

I'm trying to parse out the data so I have something like this:

WinningTeam     WinningTeamScore      LosingTeam    LosingTeamScore
-----------     ----------------      ----------    ---------------
Washington      40                    New Orleans   32
Detroit         27                    St. Louis     23

I'm stuck. I was going to use charindex to find the first space, however some city names (St. Louis, New York, etc) have spaces in the names.

Is there a way to identify the position of the first number in a string?

Thanks

like image 740
codingguy3000 Avatar asked Sep 05 '25 12:09

codingguy3000


1 Answers

Is there a way to identify the position of the first number in a string?

Yes

SELECT PATINDEX('%[0-9]%','Washington 40 New Orleans 32')

PATINDEX returns 0 if the pattern can't be found or the 1 based index of the beginning of the match otherwise.

like image 186
Martin Smith Avatar answered Sep 08 '25 11:09

Martin Smith