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)
 Substring

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-07-25 : 09:29:09
Mihamar writes "The values of a field look like "abcdefg/hij". What function should I use to get a substrig (let's say the characters following the "/") ?"

dsdeming

479 Posts

Posted - 2002-07-25 : 09:39:38
Use SUBSTRING and CHARINDEX:

SUBSTRING( columnname, CHARINDEX( '/', Columnname ) + 1, columnlength )

Note that if you just want all characters from a certain point on, there's no need to calculate the length ( argument 3 ) you pass to the substring. Just give it the length of the variable or column you're dealing with.

Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-07-25 : 09:44:40
There are loads of diffent ways to do this, here's 3

declare @str varchar(20),@val1 varchar(10),@val2 varchar(10),@val3 varchar(10)
set @str = 'abcdefg/hij'

set @val1 = RIGHT(@str,(LEN(@str)-CHARINDEX('/',@str,1)))

set @val2 = SUBSTRING(@str,(CHARINDEX('/',@str,1)+1),(LEN(@str)-CHARINDEX('/',@str,1)))

set @val3 = RIGHT(@str,CHARINDEX('/',REVERSE(@str),1)-1)

select @val1 , @val2 , @val3


HTH
Jasper Smith
Go to Top of Page

joshb
Yak Posting Veteran

52 Posts

Posted - 2002-07-25 : 09:44:57
Another way:

SELECT RIGHT(Value, LEN(Value)-CHARINDEX('/', Value))

Go to Top of Page
   

- Advertisement -