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
 Select everything one side of a dot

Author  Topic 

dbchud
Starting Member

13 Posts

Posted - 2009-08-26 : 10:46:12
Hi

Hope this makes some form of sense...

I have some values and I'm trying to select everything to the right hand side of a dot. An example value is 'hello.world' - so I'd like to select the words 'hello' and 'world' into seperate columns. Is this possible?

Had a hunt around for an answer but no luck.

Any help appreciated.

Cheers
Gary

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-08-26 : 10:58:11
[code]
declare @var1 nvarchar(20)

set @var1 = 'hello.world'

select left(@var1,charindex('.',@var1)-1), right(@var1,charindex('.',@var1)-1)

select substring(@var1,1,(charindex('.',@var1)-1)), substring(@var1,(charindex('.',@var1)+1),len(@var1)-(charindex('.',@var1)-1))
[/code]
Here's 2 ways.

EDIT: had a couple of errors in my script. Fixed now
Go to Top of Page

dbchud
Starting Member

13 Posts

Posted - 2009-08-26 : 11:27:14
Thanks, will give that a bash.
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-08-26 : 12:10:04
or:


DECLARE @var1 nvarchar(20)
SET @var1 = N'Hello.World'

SELECT PARSENAME(@var1, 2), PARSENAME(@var1, 1)
Go to Top of Page
   

- Advertisement -