locked
values split in sql RRS feed

  • Question

  • User639567535 posted
    have a column in a table

    Table 1

    Tid. name fid
    -----------------
    1. Abc. 233
    2. def. 344
    3. xyz. 455
    Table 2

    did. name FID
    -------------------------------------------
    1 Abu,def,xyz 233,344,455
    Now I want to split these fid and name from table2 in store procedure like this:

    ID name FID
    ---------------
    1. Abc. 233
    2. Def. 344
    3 xyz. 455
    Update sp

    Query:

    Create procedure as splitdata as As Begin Declare @fid varchar (500) Select name, select item as fid from spiltstring(@fid,',')) from table1 Inner join table2 on table1.fid = table2.fid

    This shows error

    Conversion failed when converting the varchar value '233,344,455' to data type int

    How can I do this?
    Sunday, January 1, 2017 8:27 AM

All replies

  • User639567535 posted
    Any help please
    Sunday, January 1, 2017 6:37 PM
  • User-1838255255 posted

    Hi Bakhtawar Ashiq,

    Conversion failed when converting the varchar value '233,344,455' to data type int

    According to this error message, as far as I know, you could try to use replace method to convert  '233,344,455' to INT type, it can replace ',' to blank, then convert it to int type.

    Sample code:

    SELECT CAST(REPLACE('233,344,455',',','') as int)
    

    Best Regards,

    Eric Du

    Tuesday, January 3, 2017 6:41 AM