parsing a string

Last Post 16 Mar 2006 12:33 PM by natasha. 3 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
natasha
New Member
New Member

--
16 Mar 2006 11:11 AM
Here is the dilemma;
trying to parse a string

eg:

'ahdjkfhjkdhjjkhjk (APTS24) jkdjfkjk'

I should be able to parse if apartment number is double digit from APTS. It could also be a alpha character.
My code works for single number (APTS1). Brain id fried at the moment.
Here is the code that works.
select
case when charindex('APTS','ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk') = 0 then ''
else substring('ahdjkfhjkdhjjkhjk (APTS1) jkdjfkjk',charindex('APTS','ahdjkfhjkdhjjkhjk (APTS1) jkdjfkjk')+4,1)

end as 'APT NUMBER'

Thanks,
Nat
nosepicker
New Member
New Member

--
16 Mar 2006 12:09 PM
It's a little messy, but this should work as long as the apartment number is always followed by a close parenthesis (")"):

select
case when charindex('APTS','ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk') = 0 then ''
else substring('ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk', charindex('APTS','ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk') +4, (charindex(')', 'ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk', charindex('APTS','ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk') +1) - charindex('APTS','ahdjkfhjkdhjjkhjk (APTS14) jkdjfkjk') -4))

end as 'APT NUMBER'

natasha
New Member
New Member

--
16 Mar 2006 12:33 PM
Thank you.
I agree it is very messy but hey at this point whatwver works. I am freaking dead tired.
Nat
SQLUSA
New Member
New Member

--
21 Mar 2006 11:53 PM
Natasha,

How many hours do you work in an 8 hours day?

In well run places, good software engineers work about 1 hour a day, daydream or attend meetings for the rest.

Kalman Toth, Database, Data Warehouse and BI Architect
http://www.sqlusa.com/orderdoubleheader/
The Best SQL Server 2005 Training in the World
You are not authorized to post a reply.

Acceptable Use Policy