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
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
add a comment |
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
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
add a comment |
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
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
mysql sql
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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
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 return10
instead of5
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
add a comment |
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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 return10
instead of5
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
add a comment |
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
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 return10
instead of5
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
add a comment |
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
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
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 return10
instead of5
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
add a comment |
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 return10
instead of5
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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