Newbie Needs Help on Substrings

  • I need to take a staff id 4 digit and return the value of 09XXXX. (XXXX being the staff Id's)

    I tried to get the Staff id in one column and then the value of 09 in another column and would manipulate it in the spreadsheet but was told I cant do that...

    Being a New Person to SQL I have banged my head for the last two days on this I think it can be done with the substring function or the case function but cant make it happen with either ...

    Any help in the right direction would be awesome...

    Thanks.

  • Roseane (4/29/2010)


    I need to take a staff id 4 digit and return the value of 09XXXX. (XXXX being the staff Id's)

    I tried to get the Staff id in one column and then the value of 09 in another column and would manipulate it in the spreadsheet but was told I cant do that...

    Being a New Person to SQL I have banged my head for the last two days on this I think it can be done with the substring function or the case function but cant make it happen with either ...

    Any help in the right direction would be awesome...

    Thanks.

    if the column contains 09XXXX and u want to seperate XXXX, the simplest way to do is

    select right(col1, 4) from table1



    Pradeep Singh

  • It sounds more like a concatenation problem rather than a substring.

    select '09' + convert(varchar(4), id) from ....

    will get you 09xxxx where xxxx is the id. If the id is already a char or varchar, you don't need the convert. If the id is NULL you may have to explicitly set the CONCAT_NULL_YIELDS_NULL option to get what you want.


    And then again, I might be wrong ...
    David Webb

  • The field Ther_id contains 1872 and I need the output to be 091872

  • So it's

    select '09' + Ther_id

    or

    select '09' + convert(varchar(4),Ther_id)

    depending on whether Ther_id is a character or numeric datatype already.


    And then again, I might be wrong ...
    David Webb

  • You are so my Hero David.... It works wonderful... Thank you.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply