Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 retrieve last word from text field

Author  Topic 

odonnellt
Starting Member

4 Posts

Posted - 2004-05-19 : 05:07:51
I need to get the last word from a text field so i can use it to sort records.
the reason for this is because we currently use a text field to store names and now want to be able to sort on the surname.

any help would be appreciated.

thanks,
tony

nr
SQLTeam MVY

12543 Posts

Posted - 2004-05-19 : 05:54:10
select right(fld,charindex(' ',reverse(fld))-1)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-05-19 : 05:57:54
You can't use RIGHT or REVERSE on a text field, can you? If Tony meant a character column then that would work otherwise it gets a whole lot messier!


Raymond
Go to Top of Page

odonnellt
Starting Member

4 Posts

Posted - 2004-05-19 : 06:04:07
I've tried the above sql casting the text field as a char. it doesn't quite work.
a lot of the fields come out blank and some return part of the surname
Go to Top of Page

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-05-19 : 06:08:52
Be aware that using CAST will only work if all your data is less than 8k. Are there trailing spaces in the data? Try using RTRIM on Nigel's command.


Raymond
Go to Top of Page

odonnellt
Starting Member

4 Posts

Posted - 2004-05-19 : 07:01:51
thanks it works now.
your help is much appreciated.
Go to Top of Page
   

- Advertisement -