T-sql subscript only numbers from string, cannot use create procedure, create table, etc

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


T-sql subscript only numbers from string, cannot use create procedure, create table, etc



Similar questions have been posted in the past, but I work in a very limited environment. The offered solutions involved using create procedure, create table, etc. None of that is available here. How can I subscript only digits (0-9) that are non-continuous from strings such as '09text10more text!@@#11' to return 091011?. Is it possible with a combination of functions, something like SELECT Funtion1(Function2(StringField)) From Schema.Table?. Data type is varchar(30). Thank you.





So, you need to be able to execute this within the context of a single query?
– csharplova
yesterday





Please read this for some tips on improving your question. It's helpful to tag database questions with both the appropriate software (MySQL, Oracle, DB2, ...) and version, e.g. sql-server-2014. Differences in syntax and features often affect the answers. Note that tsql narrows the choices, but does not specify the database.
– HABO
yesterday


sql-server-2014


tsql




1 Answer
1



This is a bit of a hack. :)



For my testing I used a variable but this will work with your field and a normal SELECT FROM query...


declare @textval nvarchar(max) = '09text10more text!@@#11'

SELECT
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,1,1))=1 THEN SUBSTRING(@textval,1,1) ELSE '' END +
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,2,1))=1 THEN SUBSTRING(@textval,2,1) ELSE '' END +
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,3,1))=1 THEN SUBSTRING(@textval,3,1) ELSE '' END +
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,4,1))=1 THEN SUBSTRING(@textval,4,1) ELSE '' END +
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,5,1))=1 THEN SUBSTRING(@textval,5,1) ELSE '' END +
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,6,1))=1 THEN SUBSTRING(@textval,6,1) ELSE '' END +
...
CASE WHEN PATINDEX('%[0-9]%', SUBSTRING(@textval,30,1))=1 THEN SUBSTRING(@textval,30,1) ELSE '' END



You basically need one case statement for every character in the field.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived