Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Validating if a varchar column contains int values

Validating if a varchar column contains int values

Scheduled Pinned Locked Moved Database
questionjson
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JacquesDP
    wrote on last edited by
    #1

    Hi, I have a column that contains varchar values, the structure that the values are built is always the same, first six characters is a string prefix, anything after the prefix must be int or it is invalid. What I want to do is subtring the column starting at character 7 for the rest of the value, but how do I validate if that value returned by the substring is int. something like if value is int, is there a way that I can do this? So lets say the column contains the following values: abcdef123321 abcdef78998745 abcdef4568547 abcdef123548/ it should only return the first 3 rows because the fourth row is invalid because of the "/" at the end Thanks in advance

    No matter how long he who laughs last laughs, he who laughs first has a head start!

    A B 2 Replies Last reply
    0
    • J JacquesDP

      Hi, I have a column that contains varchar values, the structure that the values are built is always the same, first six characters is a string prefix, anything after the prefix must be int or it is invalid. What I want to do is subtring the column starting at character 7 for the rest of the value, but how do I validate if that value returned by the substring is int. something like if value is int, is there a way that I can do this? So lets say the column contains the following values: abcdef123321 abcdef78998745 abcdef4568547 abcdef123548/ it should only return the first 3 rows because the fourth row is invalid because of the "/" at the end Thanks in advance

      No matter how long he who laughs last laughs, he who laughs first has a head start!

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      You can use the sql ISNUMERIC declare @variable varchar(100) set @variable = 'abcdef78998745' select isnumeric(substring(@variable,7,datalength(@variable)-6)) --returns 1 set @variable = 'abcdef4568547' select isnumeric(substring(@variable,7,datalength(@variable)-6)) --returns 1 set @variable = 'abcdef123548/' select isnumeric(substring(@variable,7,datalength(@variable)-6)) --returns 0 as not numeric Hope this helps

      Bob Ashfield Consultants Ltd

      J 1 Reply Last reply
      0
      • A Ashfield

        You can use the sql ISNUMERIC declare @variable varchar(100) set @variable = 'abcdef78998745' select isnumeric(substring(@variable,7,datalength(@variable)-6)) --returns 1 set @variable = 'abcdef4568547' select isnumeric(substring(@variable,7,datalength(@variable)-6)) --returns 1 set @variable = 'abcdef123548/' select isnumeric(substring(@variable,7,datalength(@variable)-6)) --returns 0 as not numeric Hope this helps

        Bob Ashfield Consultants Ltd

        J Offline
        J Offline
        JacquesDP
        wrote on last edited by
        #3

        I think this will do the trick, thanks a million.

        No matter how long he who laughs last laughs, he who laughs first has a head start!

        A 1 Reply Last reply
        0
        • J JacquesDP

          Hi, I have a column that contains varchar values, the structure that the values are built is always the same, first six characters is a string prefix, anything after the prefix must be int or it is invalid. What I want to do is subtring the column starting at character 7 for the rest of the value, but how do I validate if that value returned by the substring is int. something like if value is int, is there a way that I can do this? So lets say the column contains the following values: abcdef123321 abcdef78998745 abcdef4568547 abcdef123548/ it should only return the first 3 rows because the fourth row is invalid because of the "/" at the end Thanks in advance

          No matter how long he who laughs last laughs, he who laughs first has a head start!

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          here it is select *,substring(value,7,len(value)) as intValue from myTable where charindex('/',value) =0


          I Love T-SQL "Don't torture yourself,let the life to do it for you."

          1 Reply Last reply
          0
          • J JacquesDP

            I think this will do the trick, thanks a million.

            No matter how long he who laughs last laughs, he who laughs first has a head start!

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #5

            No problem

            Bob Ashfield Consultants Ltd

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups