Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2011
    Location
    Corpus Christi, TX
    Posts
    26

    Unanswered: Convert Columns in Table to Int from Varchar

    Hello. I have a table that imported as varchar. Most of these columns need to be in a numerical format. How can I convert a table with columns named column0 (needs to be int),column1 (stays varchar), column2(needs to be int), and column 3(needs to be int)?

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    CREATE TABLE JC01 (
       Column0		VARCHAR(9)
    ,  Column1		VARCHAR(9)
    ,  Column2		VARCHAR(9)
    ,  Column3		VARCHAR(9)
       )
    
    INSERT INTO JC01 (
       Column0, Column1, Column2, Column3
       ) SELECT      '0',      '1',  '2',  '3'
       UNION SELECT '10',     '11', '12', '13'
       UNION SELECT '20', 'Er, no', '22', '23'
    
    CREATE TABLE NextStage (
       Column0		INT
    ,  Column1		VARCHAR(9)
    ,  Column2		INT
    ,  Column3		INT
       )
    
    INSERT INTO NextStage (
       Column0, Column1, Column2, Column3
       ) SELECT Column0, Column1, Column2, Column3
          FROM JC01
    
    SELECT Column0, Column1, Column2, Column3
       FROM NextStage
       COMPUTE Sum(Column0), Sum(Column2), Sum(Column3)
    
    DROP TABLE JC01
    DROP TABLE NextStage
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Oct 2011
    Location
    Corpus Christi, TX
    Posts
    26
    Thanks. This is above my head so I have couple of questions.

    what is this doing?

    UNION SELECT '10', '11', '12', '13'
    UNION SELECT '20', 'Er, no', '22', '23'

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    The UNION operator just "glues" two result sets together.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    If justchillin wanted to convert from INT to VARCHAR, then I'd probably suggest using the String() function.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •