Skip to main content

Notifications

Community site session details

Community site session details

Session Id : VR9fWfX1N/hwqQMjtbe+jj
Power Automate - Building Flows
Answered

How to detect if a character is an integer, or not.

Like (0) ShareShare
ReportReport
Posted on 9 Oct 2020 17:34:14 by 3,340

I need to check if the first character of a string is a number.  Is there an easy way to do this?  I need to do this in an expression.  Some languages have IfError() to trap an error, or some other way?

 

This is what would work for me (if the error statement was available)

 

 

if(IfError(int(substring(item()?['ProductID'],0,1)),0,1)

 

 

 

What I do NOT want to do (but would work):

 

 

if(equals(substring(item()?['ProductID'],0,1),'0'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'1'),1,
 if(equals(substring(item()?['ProductID'],0,1),'2'),1,
 if(equals(substring(item()?['ProductID'],0,1),'3'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'4'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'5'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'6'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'7'),1, 
 if(equals(substring(item()?['ProductID'],0,1),'8'),1,
 if(equals(substring(item()?['ProductID'],0,1),'9'),1,
 0))))))))))

 

 

 

 

Thanks.

  • Clifferton Profile Picture
    94 on 01 Dec 2021 at 22:07:38
    Re: How to detect if a character is an integer, or not.

    If the 'firstyVAR' block fails, does the flow show a failure in the run history, or is that failure mitigated by the fact that there's a block set to run after a fail?

  • eliotcole Profile Picture
    4,218 Super User 2025 Season 1 on 25 Apr 2021 at 18:18:01
    Re: How to detect if a character is an integer, or not.

    I realise that you have a solution, but without getting into the deeper expression and just looking at your initial question, I initially thought of two methods:

    1. Use OR - Potentially simplify your method, firstly by making it easier to parse (visually) with a variable defining the first character separately 'firstyVAR'. Then using OR to simplify your IF tree (these are set as strings for simplicity's sake): or(equals(variables('firstyVAR'), '0'),equals(variables('firstyVAR'), '1'),equals(variables('firstyVAR'), '2'),equals(variables('firstyVAR'), '3'),equals(variables('firstyVAR'), '4'),equals(variables('firstyVAR'), '5'),equals(variables('firstyVAR'), '6'),equals(variables('firstyVAR'), '7'),equals(variables('firstyVAR'), '8'),equals(variables('firstyVAR'), '9'))
    2. Check the first character against an array, which is easier than you'd think, with a pre-set array of ["1","2","3","4","5","6","7","8","9","0"] and then performing a contains(variables('arrayVAR'), variables('firstyVAR')) expression which will return true or false dependent on the number. It's much cleaner.

     

    However, it was in the processing of that second one, that I realised that there's a super easy way to ascertain this:

     

    Run After

     

    So ... Here is the flow that I used to do it (triggerBody()['text'] is where your initial string would be):
    ignore the comment in 'intYnVAR'ignore the comment in 'intYnVAR'

     

    In order for this to work, everything depends on what happens when defining the integer variable, 'firstyVAR'. So when using the int() conversion function on the first letter of the string:

    int(substring(triggerBody()['text'], 0, 1))

    That is were you will branch:

    1. For success, if the substring managed to convert into an integer, winner winner chicken dinner.
    2. For failure, if it failed, then this is where you process the failure.

    When managing failure states, I always find it simpler to set a boolean, which we can use to move on from there. But you can perform whatever you like, obviously.

     

    The key here is clicking the dot menu on the failure branch and setting the 'Configure run after' value:

    Configure run afterConfigure run after

     

     

     

    Here you ensure that you set it to "has failed".

    has failedhas failed

     

     

     

    Then when the int() runs, if it fails, then the failure branch will run!

     

    You're not done yet, though, as your next entry has to run after EITHER of the previous branches ran. So you need to repeat the action on the unifying connector. You must go in twice to get it right, though:

    1. Once to set it to run after any eventuality in the success branch. CLICK 'Done'
    2. Once more to set it to run after any eventuality in the failure branch. CLICK 'Done'

    Once more with feelingOnce more with feeling

     

     

     

    That's it!

     

    Here's some results for proof:

    no integerno integer

     

    integerinteger

     

     

    NB - Be careful with 'Run after' as it has a knack of removing itself when you amend affecting parts of the flow. So always check your Run Afters if you have them! (or try to get them in a scope)

  • Verified answer
    martinav Profile Picture
    3,340 on 10 Oct 2020 at 14:26:53
    Re: How to detect if a character is an integer, or not.

    I gave up on this.  Ended up changing the View in SQL.  It has proper command statements for such things.

    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'Kel%') THEN 2 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'RMA%') THEN 3 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'DC%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'NW%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'BT%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'DC%') THEN 5 ELSE 
    CASE WHEN (dbo.OrderDetail.OrderNumber LIKE 'HF%') THEN 5 ELSE 
    CASE WHEN (ISNUMERIC(SUBSTRING(dbo.OrderDetail.ProductID, 1, 6)) = 1) THEN 4 ELSE 
    CASE WHEN (ISNUMERIC(LEFT(dbo.OrderDetail.ProductID, 1)) = 1 AND SUBSTRING(dbo.OrderDetail.ProductID, 2, 1) = '-') THEN 5 ELSE 
    CASE WHEN (LEFT(dbo.OrderDetail.ProductID, 1) = 'K' AND ISNUMERIC(SUBSTRING(dbo.OrderDetail.ProductID, 2, 4)) = 1) 
    THEN 4 ELSE 1 END END END END END END END END END END AS OrderType

     

  • martinav Profile Picture
    3,340 on 09 Oct 2020 at 18:52:28
    Re: How to detect if a character is an integer, or not.

    Yea, the action block method is great, but will not work in this case, because this statement is with a Select action.  I was going to be happy with the nested if(), which worked great for one condition.  However, now I have the need to detect if the first SIX characters of a string constitute a number.  That will break the lexicographical order idea.  Which was a good one, btw.  But things have become more complex.  

     

    What I am actually doing, is moving this condition statement to create another data column in a Flow Select action.   This is the statement in powerapps:

     

     

    If(StartsWith(OrderNumOB,"Kel"),2,
     StartsWith(OrderNumOB,"RMA"),3,
     StartsWith(ProductID,"HF"),5,
     StartsWith(ProductID,"DC"),5,
     StartsWith(ProductID,"NW"),5,
     IsMatch(ProductID,Match.Digit) And Mid(ProductID,2,1)="-",5,
     StartsWith(ProductID,"BT"),5,
     StartsWith(ProductID,"K") And IsNumeric(Mid(ProductID,2,1)),4,
     IsMatch(ProductID, Match.Digit & Match.Digit & Match.Digit & Match.Digit & Match.Digit & Match.Digit),4,1)

     

    This is what I have so far  in an expression.  This works perfectly at this point.  However, I need to add the last two lines from above, into the expression.

     

    if(startsWith(item()?['OrderNumOB'],'Kel'),2,
     if(startsWith(item()?['OrderNumOB'],'RMA'),3,
     if(startsWith(item()?['ProductID'],'HF'),5,
     if(startsWith(item()?['ProductID'],'DC'),5,
     if(startsWith(item()?['ProductID'],'NW'),5,
     if(startsWith(item()?['ProductID'],'BT'),5,
     if(and(
     equals(
     if(equals(substring(item()?['ProductID'],0,1),'0'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'1'),1,
     if(equals(substring(item()?['ProductID'],0,1),'2'),1,
     if(equals(substring(item()?['ProductID'],0,1),'3'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'4'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'5'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'6'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'7'),1, 
     if(equals(substring(item()?['ProductID'],0,1),'8'),1,
     if(equals(substring(item()?['ProductID'],0,1),'9'),1,
     0)))))))))),
     1),
     equals(substring(item()?['ProductID'],1,1),'-')),5,
     
    0))))))

     

  • efialttes Profile Picture
    14,756 on 09 Oct 2020 at 18:16:35
    Re: How to detect if a character is an integer, or not.

    @martinav 

    Mmmm... to trap errors in power automate you need to implement a try-catch action blocks based approach.

    https://poszytek.eu/en/microsoft-en/office-365-en/powerautomate-en/try-catch-pattern-in-microsoft-flow/

     

    I don't believe it is possible to implement it by means of a pure expression based approach, but if someone else has managed to do that, more than happy to learn one more thing

     

    Now, just guessing if a lexicographical order based expression I.e. the first character is greater or equal to character '0' and less or equal to character '9' is could help you to achieve your requirements

     

    Hope this helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,695 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,015 Most Valuable Professional

Leaderboard
Loading started