none
how to select last two words from a string...

    Question

  • I need a sql statement to get the last two words from a string....

    Example:  Table A

          1. what is your name

         2. What's your birth place

    The query should return

    1.your name

    2. Birth place..

    help please...

    Wednesday, September 04, 2013 6:11 PM

Answers

  • create table Table_A (t_id int, col varchar(200))
    Insert into table_A   Values(1,'1.what is your name'),(2,'2.What''s your birth place')
    --,(3,null)
    Select  Left(col,charindex('.',col))+Reverse(Left(Reverse(col), Charindex(' ', Reverse(col), charindex(' ',Reverse(col))+1))) from Table_A
     
    drop table table_A

    • Proposed as answer by Saeid Hasani Wednesday, September 04, 2013 7:09 PM
    • Marked as answer by Cool Tech Wednesday, September 04, 2013 7:47 PM
    Wednesday, September 04, 2013 6:41 PM
  • select
    reverse(left(reverse('what is your name'),charindex(' ',reverse('what is your name'),charindex(' ',reverse('what is your name'))+1)))
    


    Bonediggler

    • Proposed as answer by Bonediggler Wednesday, September 04, 2013 7:28 PM
    • Marked as answer by Cool Tech Wednesday, September 04, 2013 7:45 PM
    Wednesday, September 04, 2013 6:58 PM

All replies

  • create table Table_A (t_id int, col varchar(200))
    Insert into table_A   Values(1,'1.what is your name'),(2,'2.What''s your birth place')
    --,(3,null)
    Select  Left(col,charindex('.',col))+Reverse(Left(Reverse(col), Charindex(' ', Reverse(col), charindex(' ',Reverse(col))+1))) from Table_A
     
    drop table table_A

    • Proposed as answer by Saeid Hasani Wednesday, September 04, 2013 7:09 PM
    • Marked as answer by Cool Tech Wednesday, September 04, 2013 7:47 PM
    Wednesday, September 04, 2013 6:41 PM
  • select
    reverse(left(reverse('what is your name'),charindex(' ',reverse('what is your name'),charindex(' ',reverse('what is your name'))+1)))
    


    Bonediggler

    • Proposed as answer by Bonediggler Wednesday, September 04, 2013 7:28 PM
    • Marked as answer by Cool Tech Wednesday, September 04, 2013 7:45 PM
    Wednesday, September 04, 2013 6:58 PM