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
 General SQL Server Forums
 New to SQL Server Programming
 selecting a part of the data

Author  Topic 

j0shua
Starting Member

40 Posts

Posted - 2008-11-10 : 04:12:09
Is there a function that would select a specific part of a data such as

0011-black
0213-yellow
0214-yellow/green

i want to select only the black, yellow, and yellow/green
I only know how to select a specific part like the first 4 characters.
Thank you. i will appreciate your help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 04:15:43
[code]SELECT PARSENAME(REPLACE(Col,'-','.'),1) FROM Table[/code]
Go to Top of Page

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-11-10 : 04:31:08
pls try this too

select right(col,charindex('-',col)+1) from table

ok
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2008-11-10 : 04:34:01
SELECT SUBSTRING('0213-yellow',CHARINDEX('-','0213-yellow',1)+1,LEN('0213-yellow'))
Go to Top of Page

j0shua
Starting Member

40 Posts

Posted - 2008-11-10 : 04:35:40
Wow, thank you so much!!!
Go to Top of Page

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-11-10 : 04:36:24
[code]Hi sooraj, u r query ll not give correct result
try like this

Declare @t Table(Val Varchar(100))

Insert Into @t
Select '0011-black' union all
Select '0213-yellow' union all
Select '0214-yellow/green'

Select *, right(Val, Len(Val) - charindex('-',Val))
From @t[/code]


[code]"There is only one difference between a dream and an aim. A dream requires soundless sleep to see, whereas an aim requires sleepless efforts to achieve..!!"
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 04:41:39
quote:
Originally posted by soorajtnpki

pls try this too

select right(col,charindex('-',col)+1) from table

ok


it should be
select right(col,len(col)-charindex('-',col)) from table
Go to Top of Page

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-11-10 : 04:44:59
ok
but have a look at this
declare @chr varchar(30)
set @chr='sdf-fdgdg'
select right(@chr,charindex('-',@chr)+1)

will give fdgdg
wats the problem?
ok
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-10 : 04:50:44
quote:
Originally posted by soorajtnpki

ok
but have a look at this
declare @chr varchar(30)
set @chr='sdf-fdgdg'
select right(@chr,charindex('-',@chr)+1)

will give fdgdg
wats the problem?
ok


See here


declare @chr varchar(30)
set @chr='TEST-TESTING'
select right(@chr,charindex('-',@chr)+1)


Madhivanan

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

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-11-10 : 05:28:29
thanks madhivanan for ur help..

also thanks peternero for pointing my fault..

Go to Top of Page
   

- Advertisement -