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
 SQL Server Development (2000)
 String

Author  Topic 

sardinka
Posting Yak Master

142 Posts

Posted - 2005-07-18 : 10:21:31
If I have a string where I have character ':' which could be in any position. I would like to display only string before that character':'.
How do I do this?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-07-18 : 10:24:48
here's one way:

declare @str varchar(50)
set @str = 'as;dlkfj:asldjfk'

select substring(@str, 1, charindex(':', @str) - 1)

EDIT:
if it's possible that the ":" doesn't exist in the string:
select case
when charindex(':', @str) > 0 then substring(@str, 1, charindex(':', @str) - 1)
else @str
end



Be One with the Optimizer
TG
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-07-18 : 10:28:06
or
declare @str varchar(50)
set @str = 'as;dlkfj:asldjfk'

left(@str,charindex(':', @str)-1)



Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page
   

- Advertisement -