Greeshma
Greeshma

Reputation: 105

Convert String to integer and increment in MYSQL

I am new to MySQL and i am storing a version number in a column called version which is a string.I want to convert it into integer and increment by one. For example if version = 2.2.1....By running a query i want to change it to 2.2.2 is there anyone who did similar queries?

Upvotes: 0

Views: 404

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133380

You could use some string function

SELECT CONCAT(
    LEFT(your_column, LENGTH(your_column) - LOCATE('.', REVERSE(your_column))+1), 
        CAST(SUBSTRING_INDEX(your_column, '.', -1) AS UNSIGNED) +1
)

eg

SELECT CONCAT(
    LEFT("2.2.2", LENGTH("2.2.2") - LOCATE('.', REVERSE("2.2.2"))+1),  
        CAST(SUBSTRING_INDEX("2.2.2", '.', -1) AS UNSIGNED) +1
)

Upvotes: 1

Related Questions