MySQL get only numbers from result










0















I have some data that looks like below. KOPOF-ORT2 is always 20 chars long. I want to extract the 3 last characters if they are a number.



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | DI |


Desired result:



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | |


I have tried something likes this



SELECT KOPOF-ORT2, IF(substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$', 
substr(KOPOF-ORT2,18,3), '') AS Minimum FROM,,.


But is seems like it is not accepting REGEXP in the IF statement. Is there any other way I can do this?










share|improve this question
























  • It'd be a lot easier to do it in your application code. What language are you using?

    – dmikester1
    Nov 13 '18 at 15:03











  • Have you tried using a REGEX that only matches numbers, so that you don't require an IF statement at all?

    – E Smith
    Nov 13 '18 at 15:04











  • @dmikester1 Yes I know but I just wanted to see if it was possible to do in SQL first.

    – g3blv
    Nov 13 '18 at 15:06











  • @ESmith you mean to add it like WHERE substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$'? Then I guess would miss the entry with IT 33 VILLANUOVA DI.

    – g3blv
    Nov 13 '18 at 15:08






  • 1





    @g3blv No, use the REGEX_SUBSTR in the select clause so that you either get the numbers or NULL.

    – E Smith
    Nov 13 '18 at 15:31















0















I have some data that looks like below. KOPOF-ORT2 is always 20 chars long. I want to extract the 3 last characters if they are a number.



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | DI |


Desired result:



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | |


I have tried something likes this



SELECT KOPOF-ORT2, IF(substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$', 
substr(KOPOF-ORT2,18,3), '') AS Minimum FROM,,.


But is seems like it is not accepting REGEXP in the IF statement. Is there any other way I can do this?










share|improve this question
























  • It'd be a lot easier to do it in your application code. What language are you using?

    – dmikester1
    Nov 13 '18 at 15:03











  • Have you tried using a REGEX that only matches numbers, so that you don't require an IF statement at all?

    – E Smith
    Nov 13 '18 at 15:04











  • @dmikester1 Yes I know but I just wanted to see if it was possible to do in SQL first.

    – g3blv
    Nov 13 '18 at 15:06











  • @ESmith you mean to add it like WHERE substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$'? Then I guess would miss the entry with IT 33 VILLANUOVA DI.

    – g3blv
    Nov 13 '18 at 15:08






  • 1





    @g3blv No, use the REGEX_SUBSTR in the select clause so that you either get the numbers or NULL.

    – E Smith
    Nov 13 '18 at 15:31













0












0








0








I have some data that looks like below. KOPOF-ORT2 is always 20 chars long. I want to extract the 3 last characters if they are a number.



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | DI |


Desired result:



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | |


I have tried something likes this



SELECT KOPOF-ORT2, IF(substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$', 
substr(KOPOF-ORT2,18,3), '') AS Minimum FROM,,.


But is seems like it is not accepting REGEXP in the IF statement. Is there any other way I can do this?










share|improve this question
















I have some data that looks like below. KOPOF-ORT2 is always 20 chars long. I want to extract the 3 last characters if they are a number.



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | DI |


Desired result:



| KOPOF-ORT2 | Minimum |
|----------------------|---------|
| SE 10-19,60-61, 652 | 652 |
| SE 20-28,29 476 | 476 |
| IT 33 VILLANUOVA DI | |


I have tried something likes this



SELECT KOPOF-ORT2, IF(substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$', 
substr(KOPOF-ORT2,18,3), '') AS Minimum FROM,,.


But is seems like it is not accepting REGEXP in the IF statement. Is there any other way I can do this?







mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:33







g3blv

















asked Nov 13 '18 at 14:58









g3blvg3blv

61821228




61821228












  • It'd be a lot easier to do it in your application code. What language are you using?

    – dmikester1
    Nov 13 '18 at 15:03











  • Have you tried using a REGEX that only matches numbers, so that you don't require an IF statement at all?

    – E Smith
    Nov 13 '18 at 15:04











  • @dmikester1 Yes I know but I just wanted to see if it was possible to do in SQL first.

    – g3blv
    Nov 13 '18 at 15:06











  • @ESmith you mean to add it like WHERE substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$'? Then I guess would miss the entry with IT 33 VILLANUOVA DI.

    – g3blv
    Nov 13 '18 at 15:08






  • 1





    @g3blv No, use the REGEX_SUBSTR in the select clause so that you either get the numbers or NULL.

    – E Smith
    Nov 13 '18 at 15:31

















  • It'd be a lot easier to do it in your application code. What language are you using?

    – dmikester1
    Nov 13 '18 at 15:03











  • Have you tried using a REGEX that only matches numbers, so that you don't require an IF statement at all?

    – E Smith
    Nov 13 '18 at 15:04











  • @dmikester1 Yes I know but I just wanted to see if it was possible to do in SQL first.

    – g3blv
    Nov 13 '18 at 15:06











  • @ESmith you mean to add it like WHERE substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$'? Then I guess would miss the entry with IT 33 VILLANUOVA DI.

    – g3blv
    Nov 13 '18 at 15:08






  • 1





    @g3blv No, use the REGEX_SUBSTR in the select clause so that you either get the numbers or NULL.

    – E Smith
    Nov 13 '18 at 15:31
















It'd be a lot easier to do it in your application code. What language are you using?

– dmikester1
Nov 13 '18 at 15:03





It'd be a lot easier to do it in your application code. What language are you using?

– dmikester1
Nov 13 '18 at 15:03













Have you tried using a REGEX that only matches numbers, so that you don't require an IF statement at all?

– E Smith
Nov 13 '18 at 15:04





Have you tried using a REGEX that only matches numbers, so that you don't require an IF statement at all?

– E Smith
Nov 13 '18 at 15:04













@dmikester1 Yes I know but I just wanted to see if it was possible to do in SQL first.

– g3blv
Nov 13 '18 at 15:06





@dmikester1 Yes I know but I just wanted to see if it was possible to do in SQL first.

– g3blv
Nov 13 '18 at 15:06













@ESmith you mean to add it like WHERE substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$'? Then I guess would miss the entry with IT 33 VILLANUOVA DI.

– g3blv
Nov 13 '18 at 15:08





@ESmith you mean to add it like WHERE substr(KOPOF-ORT2,18,3) REGEXP '^[0-9]+$'? Then I guess would miss the entry with IT 33 VILLANUOVA DI.

– g3blv
Nov 13 '18 at 15:08




1




1





@g3blv No, use the REGEX_SUBSTR in the select clause so that you either get the numbers or NULL.

– E Smith
Nov 13 '18 at 15:31





@g3blv No, use the REGEX_SUBSTR in the select clause so that you either get the numbers or NULL.

– E Smith
Nov 13 '18 at 15:31












1 Answer
1






active

oldest

votes


















0














Try using REGEX_SUBSTR in your SELECT clause so that it returns either the numbers you'd like or NULL.



(you can paste your solution as a comment below for others to find)






share|improve this answer























  • This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

    – g3blv
    Nov 13 '18 at 17:33












  • Great! Please mark the answer as correct so that this question shows as 'answered.'

    – E Smith
    Nov 13 '18 at 18:24










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%2f53283792%2fmysql-get-only-numbers-from-result%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try using REGEX_SUBSTR in your SELECT clause so that it returns either the numbers you'd like or NULL.



(you can paste your solution as a comment below for others to find)






share|improve this answer























  • This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

    – g3blv
    Nov 13 '18 at 17:33












  • Great! Please mark the answer as correct so that this question shows as 'answered.'

    – E Smith
    Nov 13 '18 at 18:24















0














Try using REGEX_SUBSTR in your SELECT clause so that it returns either the numbers you'd like or NULL.



(you can paste your solution as a comment below for others to find)






share|improve this answer























  • This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

    – g3blv
    Nov 13 '18 at 17:33












  • Great! Please mark the answer as correct so that this question shows as 'answered.'

    – E Smith
    Nov 13 '18 at 18:24













0












0








0







Try using REGEX_SUBSTR in your SELECT clause so that it returns either the numbers you'd like or NULL.



(you can paste your solution as a comment below for others to find)






share|improve this answer













Try using REGEX_SUBSTR in your SELECT clause so that it returns either the numbers you'd like or NULL.



(you can paste your solution as a comment below for others to find)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:59









E SmithE Smith

1,7481017




1,7481017












  • This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

    – g3blv
    Nov 13 '18 at 17:33












  • Great! Please mark the answer as correct so that this question shows as 'answered.'

    – E Smith
    Nov 13 '18 at 18:24

















  • This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

    – g3blv
    Nov 13 '18 at 17:33












  • Great! Please mark the answer as correct so that this question shows as 'answered.'

    – E Smith
    Nov 13 '18 at 18:24
















This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

– g3blv
Nov 13 '18 at 17:33






This is the solution I used SELECT KOPOF-ORT2, REGEXP_SUBSTR(substr("KOPOF-ORT2",18,3), '^[0-9]+$') AS Minimum FROM,,.

– g3blv
Nov 13 '18 at 17:33














Great! Please mark the answer as correct so that this question shows as 'answered.'

– E Smith
Nov 13 '18 at 18:24





Great! Please mark the answer as correct so that this question shows as 'answered.'

– E Smith
Nov 13 '18 at 18:24

















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%2f53283792%2fmysql-get-only-numbers-from-result%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線