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 2012 Forums
 Transact-SQL (2012)
 Trim Statement

Author  Topic 

LarryDC
Starting Member

6 Posts

Posted - 2013-05-31 : 11:12:59
I have the following trim statement

(right(rtrim([RSA_Record_Key_String]),len(reverse(substring(reverse(rtrim([RSA_Record_Key_String])),(0),charindex('-',reverse(rtrim([RSA_Record_Key_String]))))))))


However some of the records don't have a - in them and just contain charactors. These records return no value How can I modify this so that it will always return a value?

Example:

1-abc-23 returns 23
1-abc Returns abc
1 is not returning anything right now and I would like it to return 1

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-31 : 11:47:58
You have to do something so the substring function gets valid arguments. Following is shorter and may be sufficient:
 STUFF('x'+[RSA_Record_Key_String],1,CHARINDEX('-',[RSA_Record_Key_String])+1,'')
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-01 : 06:10:56
http://visakhm.blogspot.in/2013/05/get-nth-positioned-string-from.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-06-02 : 13:00:20
Note that rtrim and ltrim are functions and not statements.

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -