Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all..

I have a problem in getting an integer value from a string using MySQL query.

Suppose, i have StoreId which has "ST-1 to ST-n" value and i want only to get the integer or number of the given string.

How am I suppose to get the integer using MySQL query?

Thank you!
Posted
Comments
Kornfeld Eliyahu Peter 2-Sep-14 3:19am    
If the pattern you show us here is fixed - do simple replace and cast...

For MYSQL Check this link hope this wil help you :

http://grokbase.com/t/mysql/mysql/054ph638hw/extract-numeric-value-from-a-string[^]

http://stackoverflow.com/questions/978147/how-do-you-extract-a-numerical-value-from-a-string-in-a-mysql-query[^]

In SQL Server we can do like this :

SQL
declare @var nvarchar(max)='ST-1'
SELECT LEFT(Val,PATINDEX('%[^0-9]%', Val+'a')-1) from(
    SELECT SUBSTRING(@var, PATINDEX('%[0-9]%', @var), LEN(@var)) Val
)x
 
Share this answer
 
Try something like

SQL
SELECT right(rtrim(StoreId),
             LEN(rtrim(StoreId)) - 3)
FROM TableName
 
Share this answer
 
v2
user the below query
SQL
SELECT substring(StoreId,instr(StoreId,'-'),length(StoreId)) from TableName
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900