SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to extract data between 3rd and 4th hyphen
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

danyeung
Posting Yak Master

102 Posts

Posted - 06/18/2012 :  19:22:24  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2871 Posts

Posted - 06/18/2012 :  19:45:17  Show Profile  Visit jezemine's Homepage  Reply with Quote
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

India
47031 Posts

Posted - 06/18/2012 :  22:48:50  Show Profile  Reply with Quote
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 - 06/19/2012 :  10:50:53  Show Profile  Reply with Quote
Parsename parses ".". The string I have is "-". Please advise.
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2865 Posts

Posted - 06/19/2012 :  10:56:13  Show Profile  Reply with Quote
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

USA
15557 Posts

Posted - 06/19/2012 :  10:56:20  Show Profile  Visit robvolk's Homepage  Reply with Quote
SELECT PARSENAME(REPLACE(Left(GL.GLAccount, 25),'-','.'),2)
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2865 Posts

Posted - 06/19/2012 :  10:58:02  Show Profile  Reply with Quote
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 - 06/19/2012 :  11:04:26  Show Profile  Reply with 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)

Edited by - danyeung on 06/19/2012 11:31:57
Go to Top of Page

robvolk
Most Valuable Yak

USA
15557 Posts

Posted - 06/19/2012 :  11:22:11  Show Profile  Visit robvolk's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2865 Posts

Posted - 06/19/2012 :  11:49:47  Show Profile  Reply with Quote
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 - 06/19/2012 :  11:55:01  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2865 Posts

Posted - 06/19/2012 :  11:56:03  Show Profile  Reply with Quote
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

India
47031 Posts

Posted - 06/19/2012 :  15:21:15  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000