Convert a string with operation to int using sql query The Next CEO of Stack OverflowSumming a comma separated column in MySQL 4 (not 5)Insert into … values ( SELECT … FROM … )How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerCan I concatenate multiple MySQL rows into one field?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or resultSQL select only rows with max value on a columnHow to get the sizes of the tables of a MySQL database?Convert INT to VARCHAR SQL

What was the first Unix version to run on a microcomputer?

Anatomically Correct Strange Women In Ponds Distributing Swords

Why do remote companies require working in the US?

Written every which way

Do I need to enable Dev Hub in my PROD Org?

Is it professional to write unrelated content in an almost-empty email?

Limits on contract work without pre-agreed price/contract (UK)

Multiple labels for a single equation

Contours of a clandestine nature

How to count occurrences of text in a file?

Several mode to write the symbol of a vector

How does the mv command work with external drives?

How do I reset passwords on multiple websites easily?

How powerful is the invisibility granted by the Gloom Stalker ranger's Umbral Sight feature?

Make solar eclipses exceedingly rare, but still have new moons

Are there any unintended negative consequences to allowing PCs to gain multiple levels at once in a short milestone-XP game?

How do scammers retract money, while you can’t?

Can we say or write : "No, it'sn't"?

Has this building technique been used in an official set?

What does "Its cash flow is deeply negative" mean?

What can we do to stop prior company from asking us questions?

In excess I'm lethal

Indicator light circuit

How to safely derail a train during transit?



Convert a string with operation to int using sql query



The Next CEO of Stack OverflowSumming a comma separated column in MySQL 4 (not 5)Insert into … values ( SELECT … FROM … )How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerCan I concatenate multiple MySQL rows into one field?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or resultSQL select only rows with max value on a columnHow to get the sizes of the tables of a MySQL database?Convert INT to VARCHAR SQL










1















persons is a tinytext field and can contain "4 + 2", "4+2", "4 +2", "5" or "" and so on.
I would like to select and int like 6, 6, 6, 5 and 0 from that MySQL 5.6 Table.



Tried this without success:



SELECT CAST(persons AS INT) FROM Table
SELECT CONVERT(INT, persons ) FROM Table









share|improve this question



















  • 3





    With 2K rep you should already know how-to-ask a question on SO.

    – Paul Spiegel
    Mar 21 at 17:01











  • so you want magic to happen? how many operations do you have in those strings? what is the list of operations? -+/* do you want to support braces?

    – Alex
    Mar 21 at 17:05











  • No it's only + Operation or just a number or empty. It's an old database that needs to be migrated. Not looking for magic, but you never know if there is something you missed. If there is no solution will do it on server side.

    – Mike
    Mar 21 at 17:07











  • Possible duplicate of Summing a comma separated column in MySQL 4 (not 5)

    – Alex
    Mar 21 at 17:12















1















persons is a tinytext field and can contain "4 + 2", "4+2", "4 +2", "5" or "" and so on.
I would like to select and int like 6, 6, 6, 5 and 0 from that MySQL 5.6 Table.



Tried this without success:



SELECT CAST(persons AS INT) FROM Table
SELECT CONVERT(INT, persons ) FROM Table









share|improve this question



















  • 3





    With 2K rep you should already know how-to-ask a question on SO.

    – Paul Spiegel
    Mar 21 at 17:01











  • so you want magic to happen? how many operations do you have in those strings? what is the list of operations? -+/* do you want to support braces?

    – Alex
    Mar 21 at 17:05











  • No it's only + Operation or just a number or empty. It's an old database that needs to be migrated. Not looking for magic, but you never know if there is something you missed. If there is no solution will do it on server side.

    – Mike
    Mar 21 at 17:07











  • Possible duplicate of Summing a comma separated column in MySQL 4 (not 5)

    – Alex
    Mar 21 at 17:12













1












1








1








persons is a tinytext field and can contain "4 + 2", "4+2", "4 +2", "5" or "" and so on.
I would like to select and int like 6, 6, 6, 5 and 0 from that MySQL 5.6 Table.



Tried this without success:



SELECT CAST(persons AS INT) FROM Table
SELECT CONVERT(INT, persons ) FROM Table









share|improve this question
















persons is a tinytext field and can contain "4 + 2", "4+2", "4 +2", "5" or "" and so on.
I would like to select and int like 6, 6, 6, 5 and 0 from that MySQL 5.6 Table.



Tried this without success:



SELECT CAST(persons AS INT) FROM Table
SELECT CONVERT(INT, persons ) FROM Table






mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 17:14







Mike

















asked Mar 21 at 16:59









MikeMike

2,08131949




2,08131949







  • 3





    With 2K rep you should already know how-to-ask a question on SO.

    – Paul Spiegel
    Mar 21 at 17:01











  • so you want magic to happen? how many operations do you have in those strings? what is the list of operations? -+/* do you want to support braces?

    – Alex
    Mar 21 at 17:05











  • No it's only + Operation or just a number or empty. It's an old database that needs to be migrated. Not looking for magic, but you never know if there is something you missed. If there is no solution will do it on server side.

    – Mike
    Mar 21 at 17:07











  • Possible duplicate of Summing a comma separated column in MySQL 4 (not 5)

    – Alex
    Mar 21 at 17:12












  • 3





    With 2K rep you should already know how-to-ask a question on SO.

    – Paul Spiegel
    Mar 21 at 17:01











  • so you want magic to happen? how many operations do you have in those strings? what is the list of operations? -+/* do you want to support braces?

    – Alex
    Mar 21 at 17:05











  • No it's only + Operation or just a number or empty. It's an old database that needs to be migrated. Not looking for magic, but you never know if there is something you missed. If there is no solution will do it on server side.

    – Mike
    Mar 21 at 17:07











  • Possible duplicate of Summing a comma separated column in MySQL 4 (not 5)

    – Alex
    Mar 21 at 17:12







3




3





With 2K rep you should already know how-to-ask a question on SO.

– Paul Spiegel
Mar 21 at 17:01





With 2K rep you should already know how-to-ask a question on SO.

– Paul Spiegel
Mar 21 at 17:01













so you want magic to happen? how many operations do you have in those strings? what is the list of operations? -+/* do you want to support braces?

– Alex
Mar 21 at 17:05





so you want magic to happen? how many operations do you have in those strings? what is the list of operations? -+/* do you want to support braces?

– Alex
Mar 21 at 17:05













No it's only + Operation or just a number or empty. It's an old database that needs to be migrated. Not looking for magic, but you never know if there is something you missed. If there is no solution will do it on server side.

– Mike
Mar 21 at 17:07





No it's only + Operation or just a number or empty. It's an old database that needs to be migrated. Not looking for magic, but you never know if there is something you missed. If there is no solution will do it on server side.

– Mike
Mar 21 at 17:07













Possible duplicate of Summing a comma separated column in MySQL 4 (not 5)

– Alex
Mar 21 at 17:12





Possible duplicate of Summing a comma separated column in MySQL 4 (not 5)

– Alex
Mar 21 at 17:12












3 Answers
3






active

oldest

votes


















3














If + is the only operator and it appears once, then:



select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value





share|improve this answer























  • This looks good so far. Testing it further...

    – Mike
    Mar 21 at 17:29











  • Works! I knew there must be a way. Thanks!

    – Mike
    Mar 21 at 17:35


















1














use SUBSTRING_INDEX



 select SUBSTRING_INDEX(col , "+", 1)+ SUBSTRING_INDEX(col , "+", -1) as col1
from cte where col like '%+%'
union all
select SUBSTRING_INDEX(col , "+", 1) from cte where col not like '%+%'


output



 col1
6
6
6
5


the upper solution will work only for your sample data
demo link






share|improve this answer

























  • I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

    – Mike
    Mar 21 at 17:04











  • @Mike . . . This will also return 10 instead of 5 for '5'.

    – Gordon Linoff
    Mar 21 at 17:27











  • @GordonLinoff i just updated answer for only sample data

    – Zaynul Abadin Tuhin
    Mar 21 at 17:34


















0














What database are you using? You may need to use something db specific. e.g. in oracle you can do:



select dbms_aw.eval_number ('4+2') from dual


It will return 6.



Generally speaking - using dynamic SQL you can easily achieve this.






share|improve this answer

























  • It's MySQL Version 5.6

    – Mike
    Mar 21 at 17:10











  • I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

    – Saad Ahmad
    Mar 21 at 17:57











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285603%2fconvert-a-string-with-operation-to-int-using-sql-query%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














If + is the only operator and it appears once, then:



select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value





share|improve this answer























  • This looks good so far. Testing it further...

    – Mike
    Mar 21 at 17:29











  • Works! I knew there must be a way. Thanks!

    – Mike
    Mar 21 at 17:35















3














If + is the only operator and it appears once, then:



select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value





share|improve this answer























  • This looks good so far. Testing it further...

    – Mike
    Mar 21 at 17:29











  • Works! I knew there must be a way. Thanks!

    – Mike
    Mar 21 at 17:35













3












3








3







If + is the only operator and it appears once, then:



select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value





share|improve this answer













If + is the only operator and it appears once, then:



select (case when col like '%+%'
then substring_index(col, '+', 1) + substring_index(replace(col, ' ', ''), '+', -1)
else col + 0
end) as added_value






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 21 at 17:26









Gordon LinoffGordon Linoff

792k36316419




792k36316419












  • This looks good so far. Testing it further...

    – Mike
    Mar 21 at 17:29











  • Works! I knew there must be a way. Thanks!

    – Mike
    Mar 21 at 17:35

















  • This looks good so far. Testing it further...

    – Mike
    Mar 21 at 17:29











  • Works! I knew there must be a way. Thanks!

    – Mike
    Mar 21 at 17:35
















This looks good so far. Testing it further...

– Mike
Mar 21 at 17:29





This looks good so far. Testing it further...

– Mike
Mar 21 at 17:29













Works! I knew there must be a way. Thanks!

– Mike
Mar 21 at 17:35





Works! I knew there must be a way. Thanks!

– Mike
Mar 21 at 17:35













1














use SUBSTRING_INDEX



 select SUBSTRING_INDEX(col , "+", 1)+ SUBSTRING_INDEX(col , "+", -1) as col1
from cte where col like '%+%'
union all
select SUBSTRING_INDEX(col , "+", 1) from cte where col not like '%+%'


output



 col1
6
6
6
5


the upper solution will work only for your sample data
demo link






share|improve this answer

























  • I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

    – Mike
    Mar 21 at 17:04











  • @Mike . . . This will also return 10 instead of 5 for '5'.

    – Gordon Linoff
    Mar 21 at 17:27











  • @GordonLinoff i just updated answer for only sample data

    – Zaynul Abadin Tuhin
    Mar 21 at 17:34















1














use SUBSTRING_INDEX



 select SUBSTRING_INDEX(col , "+", 1)+ SUBSTRING_INDEX(col , "+", -1) as col1
from cte where col like '%+%'
union all
select SUBSTRING_INDEX(col , "+", 1) from cte where col not like '%+%'


output



 col1
6
6
6
5


the upper solution will work only for your sample data
demo link






share|improve this answer

























  • I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

    – Mike
    Mar 21 at 17:04











  • @Mike . . . This will also return 10 instead of 5 for '5'.

    – Gordon Linoff
    Mar 21 at 17:27











  • @GordonLinoff i just updated answer for only sample data

    – Zaynul Abadin Tuhin
    Mar 21 at 17:34













1












1








1







use SUBSTRING_INDEX



 select SUBSTRING_INDEX(col , "+", 1)+ SUBSTRING_INDEX(col , "+", -1) as col1
from cte where col like '%+%'
union all
select SUBSTRING_INDEX(col , "+", 1) from cte where col not like '%+%'


output



 col1
6
6
6
5


the upper solution will work only for your sample data
demo link






share|improve this answer















use SUBSTRING_INDEX



 select SUBSTRING_INDEX(col , "+", 1)+ SUBSTRING_INDEX(col , "+", -1) as col1
from cte where col like '%+%'
union all
select SUBSTRING_INDEX(col , "+", 1) from cte where col not like '%+%'


output



 col1
6
6
6
5


the upper solution will work only for your sample data
demo link







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 17:33

























answered Mar 21 at 17:01









Zaynul Abadin TuhinZaynul Abadin Tuhin

18.3k21134




18.3k21134












  • I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

    – Mike
    Mar 21 at 17:04











  • @Mike . . . This will also return 10 instead of 5 for '5'.

    – Gordon Linoff
    Mar 21 at 17:27











  • @GordonLinoff i just updated answer for only sample data

    – Zaynul Abadin Tuhin
    Mar 21 at 17:34

















  • I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

    – Mike
    Mar 21 at 17:04











  • @Mike . . . This will also return 10 instead of 5 for '5'.

    – Gordon Linoff
    Mar 21 at 17:27











  • @GordonLinoff i just updated answer for only sample data

    – Zaynul Abadin Tuhin
    Mar 21 at 17:34
















I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

– Mike
Mar 21 at 17:04





I'm afraid this won't work because "persons" can also be "4+2", "4 + 2" or "4+ 2" and so on. I need something like an operation interpretation.

– Mike
Mar 21 at 17:04













@Mike . . . This will also return 10 instead of 5 for '5'.

– Gordon Linoff
Mar 21 at 17:27





@Mike . . . This will also return 10 instead of 5 for '5'.

– Gordon Linoff
Mar 21 at 17:27













@GordonLinoff i just updated answer for only sample data

– Zaynul Abadin Tuhin
Mar 21 at 17:34





@GordonLinoff i just updated answer for only sample data

– Zaynul Abadin Tuhin
Mar 21 at 17:34











0














What database are you using? You may need to use something db specific. e.g. in oracle you can do:



select dbms_aw.eval_number ('4+2') from dual


It will return 6.



Generally speaking - using dynamic SQL you can easily achieve this.






share|improve this answer

























  • It's MySQL Version 5.6

    – Mike
    Mar 21 at 17:10











  • I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

    – Saad Ahmad
    Mar 21 at 17:57















0














What database are you using? You may need to use something db specific. e.g. in oracle you can do:



select dbms_aw.eval_number ('4+2') from dual


It will return 6.



Generally speaking - using dynamic SQL you can easily achieve this.






share|improve this answer

























  • It's MySQL Version 5.6

    – Mike
    Mar 21 at 17:10











  • I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

    – Saad Ahmad
    Mar 21 at 17:57













0












0








0







What database are you using? You may need to use something db specific. e.g. in oracle you can do:



select dbms_aw.eval_number ('4+2') from dual


It will return 6.



Generally speaking - using dynamic SQL you can easily achieve this.






share|improve this answer















What database are you using? You may need to use something db specific. e.g. in oracle you can do:



select dbms_aw.eval_number ('4+2') from dual


It will return 6.



Generally speaking - using dynamic SQL you can easily achieve this.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 17:12









Rust

4,33462450




4,33462450










answered Mar 21 at 17:08









Saad AhmadSaad Ahmad

726




726












  • It's MySQL Version 5.6

    – Mike
    Mar 21 at 17:10











  • I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

    – Saad Ahmad
    Mar 21 at 17:57

















  • It's MySQL Version 5.6

    – Mike
    Mar 21 at 17:10











  • I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

    – Saad Ahmad
    Mar 21 at 17:57
















It's MySQL Version 5.6

– Mike
Mar 21 at 17:10





It's MySQL Version 5.6

– Mike
Mar 21 at 17:10













I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

– Saad Ahmad
Mar 21 at 17:57





I dont think MySQL allows dynamic SQL inside functions. So simple route of simply letting the SQL engine do it for you will not be available. Based on your needs if you can live with a stored procedure that can certainly call dynamic SQL. Or you can create a function that acytually parses string on +/*- etc. and has a switch statement etc. to compute the result. Not very complex.

– Saad Ahmad
Mar 21 at 17:57

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285603%2fconvert-a-string-with-operation-to-int-using-sql-query%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현