How to Split a char field?

  • Hi

    I do not know if it is possible but, I need find how to split a fleld in SQL server. I need to move the last three characters to another field on their own.

    Is there a way to do this?  

    the field is char (60) but the last three characters are not always on the same string position.

    Thanks

     

    AB

     

  • select substring(companyname,len(companyname) - 2,len(companyname)),companyname from customers

     

    this is a query that returns the last three characters from northwind database

  • I presume you just need to trim off the trailing spaces before you grab the last three characters:

     

    declare @Table table (col1 char(60), Col2 char(60))

    insert into @Table (Col1) values ('Test Test     123         ')

    insert into @Table (Col1) values ('Test Test Test     123                    ')

    insert into @Table (Col1) values ('Test Test Test  Test        123               ')

    select right(rtrim(Col1),3) from @Table

    Update @Table set Col2 = right(rtrim(Col1),3)

    select * from @table

     

    David

  • SELECT RIGHT(companyname, 3) AS lastThreeChars,companyname FROM Customers







    **ASCII stupid question, get a stupid ANSI !!!**

  • Thanks guys,  Got it! and it works 

    I can actually do it either way.

     

Viewing 5 posts - 1 through 4 (of 4 total)

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