Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Selecting data from a string for a join

 
   Database Help (Home) -> MSEQ RSS
Next:  Magnetic Card Reader  
Author Message
Stan G.

External


Since: Oct 17, 2008
Posts: 5



(Msg. 1) Posted: Fri Oct 17, 2008 1:27 pm
Post subject: Selecting data from a string for a join
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I have a string field that has concatenated data loaded into it. I need to
be able to do a join using the characters at the beginning of the field that
ALWAYS are followed by 1 space. There may be 4, 5 or 6 characters. The
field looks like this:
"AA-1 nnnnnnnnn" or "BB-99 xxxxxxxxxx" or "CC-888 yyyyyyyyyyyyyyy". I need
to be able to grab that first piece before the space as the matching field on
a join. What is the syntax necessary to isolate that beginning portion of
the string so that I can end up being able to join another table on AA-1 or
BB-99 or CC-888?
Thanks for any help.

 >> Stay informed about: Selecting data from a string for a join 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 2) Posted: Fri Oct 17, 2008 7:26 pm
Post subject: Re: Selecting data from a string for a join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 17 Oct 2008 13:27:00 -0700, Stan G. wrote:

>I have a string field that has concatenated data loaded into it. I need to
>be able to do a join using the characters at the beginning of the field that
>ALWAYS are followed by 1 space. There may be 4, 5 or 6 characters. The
>field looks like this:
>"AA-1 nnnnnnnnn" or "BB-99 xxxxxxxxxx" or "CC-888 yyyyyyyyyyyyyyy". I need
>to be able to grab that first piece before the space as the matching field on
>a join. What is the syntax necessary to isolate that beginning portion of
>the string so that I can end up being able to join another table on AA-1 or
>BB-99 or CC-888?
>Thanks for any help.

Hi Stan,

Note that this is a bad table design. In a relational database, each
piece of information should be in its own column. You might, in some
circumstances, add a computed column to represent the concatenation of
some other columns, but the base data should be "atomic" (i.e., exactly
one value per cell, no more).

However, if you're stuck with this design you can use
LEFT(ColumnName, CHARINDEX(' ', ColumnName) - 1)
to extract the characters up to the first space. Replace "ColumnName"
with the actual name of your column, and be prepared to see lousy
performance.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

 >> Stay informed about: Selecting data from a string for a join 
Back to top
Login to vote
Stan G.

External


Since: Oct 17, 2008
Posts: 5



(Msg. 3) Posted: Sat Oct 18, 2008 6:27 am
Post subject: Re: Selecting data from a string for a join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hugo,
Thanks for the help. Even at my level, I know this is a bad design. This
is a third party package and we have no real control over some of the design
shortcomings. For what we need to do though, your approach is all I need.
Thanks again!
Stan

"Hugo Kornelis" wrote:

> On Fri, 17 Oct 2008 13:27:00 -0700, Stan G. wrote:
>
> >I have a string field that has concatenated data loaded into it. I need to
> >be able to do a join using the characters at the beginning of the field that
> >ALWAYS are followed by 1 space. There may be 4, 5 or 6 characters. The
> >field looks like this:
> >"AA-1 nnnnnnnnn" or "BB-99 xxxxxxxxxx" or "CC-888 yyyyyyyyyyyyyyy". I need
> >to be able to grab that first piece before the space as the matching field on
> >a join. What is the syntax necessary to isolate that beginning portion of
> >the string so that I can end up being able to join another table on AA-1 or
> >BB-99 or CC-888?
> >Thanks for any help.
>
> Hi Stan,
>
> Note that this is a bad table design. In a relational database, each
> piece of information should be in its own column. You might, in some
> circumstances, add a computed column to represent the concatenation of
> some other columns, but the base data should be "atomic" (i.e., exactly
> one value per cell, no more).
>
> However, if you're stuck with this design you can use
> LEFT(ColumnName, CHARINDEX(' ', ColumnName) - 1)
> to extract the characters up to the first space. Replace "ColumnName"
> with the actual name of your column, and be prepared to see lousy
> performance.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
>
 >> Stay informed about: Selecting data from a string for a join 
Back to top
Login to vote
Stan G.

External


Since: Oct 17, 2008
Posts: 5



(Msg. 4) Posted: Sat Oct 18, 2008 6:50 am
Post subject: Re: Selecting data from a string for a join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hugo,
Spoke a little too soon. I tried this technique and I see the results flash
in the query pane briefly and then get "Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function." Is this data
related in some way?
Stan


"Hugo Kornelis" wrote:

> On Fri, 17 Oct 2008 13:27:00 -0700, Stan G. wrote:
>
> >I have a string field that has concatenated data loaded into it. I need to
> >be able to do a join using the characters at the beginning of the field that
> >ALWAYS are followed by 1 space. There may be 4, 5 or 6 characters. The
> >field looks like this:
> >"AA-1 nnnnnnnnn" or "BB-99 xxxxxxxxxx" or "CC-888 yyyyyyyyyyyyyyy". I need
> >to be able to grab that first piece before the space as the matching field on
> >a join. What is the syntax necessary to isolate that beginning portion of
> >the string so that I can end up being able to join another table on AA-1 or
> >BB-99 or CC-888?
> >Thanks for any help.
>
> Hi Stan,
>
> Note that this is a bad table design. In a relational database, each
> piece of information should be in its own column. You might, in some
> circumstances, add a computed column to represent the concatenation of
> some other columns, but the base data should be "atomic" (i.e., exactly
> one value per cell, no more).
>
> However, if you're stuck with this design you can use
> LEFT(ColumnName, CHARINDEX(' ', ColumnName) - 1)
> to extract the characters up to the first space. Replace "ColumnName"
> with the actual name of your column, and be prepared to see lousy
> performance.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
>
 >> Stay informed about: Selecting data from a string for a join 
Back to top
Login to vote
Stan G.

External


Since: Oct 17, 2008
Posts: 5



(Msg. 5) Posted: Sat Oct 18, 2008 7:05 am
Post subject: Re: Selecting data from a string for a join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Looks like I needed a ',' between the ) and the -1...correct?

"Hugo Kornelis" wrote:

> On Fri, 17 Oct 2008 13:27:00 -0700, Stan G. wrote:
>
> >I have a string field that has concatenated data loaded into it. I need to
> >be able to do a join using the characters at the beginning of the field that
> >ALWAYS are followed by 1 space. There may be 4, 5 or 6 characters. The
> >field looks like this:
> >"AA-1 nnnnnnnnn" or "BB-99 xxxxxxxxxx" or "CC-888 yyyyyyyyyyyyyyy". I need
> >to be able to grab that first piece before the space as the matching field on
> >a join. What is the syntax necessary to isolate that beginning portion of
> >the string so that I can end up being able to join another table on AA-1 or
> >BB-99 or CC-888?
> >Thanks for any help.
>
> Hi Stan,
>
> Note that this is a bad table design. In a relational database, each
> piece of information should be in its own column. You might, in some
> circumstances, add a computed column to represent the concatenation of
> some other columns, but the base data should be "atomic" (i.e., exactly
> one value per cell, no more).
>
> However, if you're stuck with this design you can use
> LEFT(ColumnName, CHARINDEX(' ', ColumnName) - 1)
> to extract the characters up to the first space. Replace "ColumnName"
> with the actual name of your column, and be prepared to see lousy
> performance.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
>
 >> Stay informed about: Selecting data from a string for a join 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 6) Posted: Sat Oct 18, 2008 7:25 pm
Post subject: Re: Selecting data from a string for a join [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sat, 18 Oct 2008 06:50:00 -0700, Stan G. wrote:

>Hugo,
>Spoke a little too soon. I tried this technique and I see the results flash
>in the query pane briefly and then get "Msg 536, Level 16, State 3, Line 1
>Invalid length parameter passed to the substring function." Is this data
>related in some way?

Hi Stan,

That means that there has to be some data with no embedded spaces. The
CHARINDEX will return 0, the - 1 (that is intended to exclude the space
itself) reduces this to -1, which is indeed an invalid length parameter
for the LEFT function (which, based on the text of the error, is
obviously implemented internally as a simplified version of SUBSTRING).

Assuming you'll need the whole string if no space is present, you'll
have to use this, more complicated expression:

CASE WHEN ColumnName LIKE '% %'
THEN LEFT(ColumnName, CHARINDEX(' ', ColumnName) - 1)
ELSE ColumnName
END

(And if, for values with no embedded space, you need to use something
else, you'll have to change the ELSE part of the above CASE expression).

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Selecting data from a string for a join 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Selecting Quarter and Year Parameter - I am working on a report where the end user would like to say giving me data for quarter 1 of 2007,ect. The data in my table has the year and date fields. How do I begin to start this query? I know I can say @Year for the year parameter, but I am no...

Set default for null values in unmatched rows of left join - Is there an easy way to set a default value for values in unmatched rows that return null I wouldlike to return 0 for fields that return null in a left join

Updating data - Hello again, ive got a table with a column that normally contains a number, for example, 4057. But there are some that finishes with an a, for example, 4057a. I want to quit the "a" and place a "1" instead, so in the last example, ...

Producing data from querying three tables - Hi all, I'm trying to run a query that produces data from three tables, in essence these are Location, Category and BannerPeriod, They all have a secondary key of SiteID and are linked by this, really there are two stages to this query, Stage 1..

Error converting data type nvarchar to datetime - Good morning :) * Using QL Server 2005 I have created a stored procedure (first time I have done this). It worked fine when I only had 1 variable (the ProjectCode variable). However when I add in the PostedDate variable and try and use it I get the ...
   Database Help (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]