Sql server query help!

Discussion in 'Databases' started by fhirzall, Sep 25, 2006.

  1. #1
    Hi everyone! Ok so I have 2 columns, one is a Name column, the other is a foreign key. In my Name column, this is the format of all the records in it :
    number-Name
    number ranges from like 1 to 1000, is there any way I can basically extract these numbers before each name, insert it into the foreign key column, then remove the number and the hyphen?
    I think I might need to use a regular expression, but I'm not so good at those.
    I'm not sure if I explained that right, let me know if you need me to explain more! Thank you.
     
    fhirzall, Sep 25, 2006 IP
  2. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would create a Cursor to do it. If you look in SQL help you will find how to create a cursor.

    A cursor step throw every record, you can then use
    select SUBSTRING(name,CHARINDEX("-",namecol)) as DBID,namecol from table

    as a question, then update with update table set IDcol=@dbid where name=@namecol

    I dont have sql server here so i can not test the above, but something like that should work. Make sure you dont lock the record with the cursor, you can not update a locked record.
     
    ThomasNederman, Sep 26, 2006 IP