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 2008 Forums
 Transact-SQL (2008)
 How to extract data between 3rd and 4th hyphen

Author  Topic 

danyeung
Posting Yak Master

102 Posts

Posted - 2012-06-18 : 19:22:24
In the following, how do I extract data between the 3rd and 4th hyphen in case else? Thanks.

CASE Left(GL.GLAccount, 25)
WHEN 'CPB01-7000000-1910-CP0289' THEN SUBSTRING(GL.GLAccount, 14, 4)
WHEN 'CAN02-7002000-1910-CP0278' THEN SUBSTRING(GL.GLAccount, 14, 4)
ELSE ???
END as GlAccount2,

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2012-06-18 : 19:45:17
can use parsename for this. see: http://weblogs.sqlteam.com/jeffs/archive/2003/09/30/195.aspx


elsasoft.org
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 22:48:50
only if GLAccount contains maximum of 4 parts

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

danyeung
Posting Yak Master

102 Posts

Posted - 2012-06-19 : 10:50:53
Parsename parses ".". The string I have is "-". Please advise.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-06-19 : 10:56:13
Use REPLACE first

PARSENAME(REPLACE(GLAccount,'-','.'),2)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-19 : 10:56:20
SELECT PARSENAME(REPLACE(Left(GL.GLAccount, 25),'-','.'),2)
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-06-19 : 10:58:02
It took you seven seconds to type SELECT?

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

danyeung
Posting Yak Master

102 Posts

Posted - 2012-06-19 : 11:04:26
Yes, I thought about using replace after posted. You guys are quick. :)

However, I got all null with he following.

declare @str as varchar(max)
set @str = 'be CPB01-7000000-1910-CP0289-880-000-254-000'

select parsename(replace(@str, '-', '.'), 1)
select parsename(replace(@str, '-', '.'), 2)
select parsename(replace(@str, '-', '.'), 3)
select parsename(replace(@str, '-', '.'), 4)
select parsename(replace(@str, '-', '.'), 5)
select parsename(replace(@str, '-', '.'), 6)
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-19 : 11:22:11
quote:
Originally posted by jimf

It took you seven seconds to type SELECT?
Hey, six letters and a space, one second each. I put care and thought and uh, what was I gonna say, um, other quality stuff in my typing!
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-06-19 : 11:49:47
quote:

Yes, I thought about using replace after posted. You guys are quick. :)

However, I got all null with he following.

declare @str as varchar(max)
set @str = 'be CPB01-7000000-1910-CP0289-880-000-254-000'

select parsename(replace(@str, '-', '.'), 1)
select parsename(replace(@str, '-', '.'), 2)
select parsename(replace(@str, '-', '.'), 3)
select parsename(replace(@str, '-', '.'), 4)
select parsename(replace(@str, '-', '.'), 5)
select parsename(replace(@str, '-', '.'), 6)




See Visakh's post
quote:

Posted - 06/18/2012 : 22:48:50
--------------------------------------------------------------------------------
only if GLAccount contains maximum of 4 parts




Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

danyeung
Posting Yak Master

102 Posts

Posted - 2012-06-19 : 11:55:01
quote:
Originally posted by danyeung

I got all null with he following.

declare @str as varchar(max)
set @str = 'be CPB01-7000000-1910-CP0289-880-000-254-000'

select parsename(replace(@str, '-', '.'), 1)
select parsename(replace(@str, '-', '.'), 2)
select parsename(replace(@str, '-', '.'), 3)
select parsename(replace(@str, '-', '.'), 4)
select parsename(replace(@str, '-', '.'), 5)
select parsename(replace(@str, '-', '.'), 6)



This example doesn't use the GL Account. I expect to see "000" for 1, "254" for 2, "000" for 3, "880" for 4, and so on. Instead, I got all null.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-06-19 : 11:56:03
You could use Visakh's ParseValues function which will turn your string into a table, and then select the value where ID = 4

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-19 : 15:21:15
here it is

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -