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 2005 Forums
 Transact-SQL (2005)
 SQL charindex problem

Author  Topic 

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2010-01-17 : 05:08:56
Hi, I got the split function working together with getting the first row value.

This works fine but it does work on getting the first row value when there is no "," in value to split then i get error incorrect parameter.

set @Firstval = substring(@str,1,charindex(',',@str)-1)

declare @str varchar(100)
set @str = (select User from dbo.fcUser where username = 'test')
declare @firstval varchar(100)
set @Firstval = substring(@str,1,charindex(',',@str)-1)
select @Firstval as DefaultUser

Please help

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2010-01-17 : 05:55:40
got help :)

declare @str varchar(100)
SET @str = 'Test'
declare @firstval varchar(100)
DECLARE @pos int;
SET @pos = charindex(',',@str)
IF @pos = 0
SET @pos = LEN(@str) + 1
set @Firstval = substring(@str,1,@pos - 1)
select @Firstval as DefaultUser
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-18 : 02:37:38
<<
set @str = (select User from dbo.fcUser where username = 'test')
>>

Note that if the query returns more than a value, it would throw error

Madhivanan

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

- Advertisement -