Solved

join text to int and separate by commas

Posted on 2011-02-24
4
305 Views
Last Modified: 2012-05-11
CREATE TABLE [dbo].[sitedata](
      [productid] [int] NULL,
      [pathid] [int] NULL,
      [accessoryid] [int] NULL,
      [alternatecolors] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [description] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [specifications] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [features] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [itemincludes] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [accessories] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [infoandguides] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [rebateinfo] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [sunshineactive] [int] NULL,
      [sunshineprice] [money] NULL,
      [sunshinefeed] [int] NULL,
      [pyxisactive] [int] NULL,
      [pyxisprice] [money] NULL,
      [pyxisfeed] [int] NULL,
      [hdewactive] [int] NULL,
      [hdewprice] [money] NULL,
      [hdewfeed] [int] NULL,
      [shippingtype] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [shippingpriceground] [money] NULL,
      [shippingprice3day] [money] NULL,
      [shippingprice2day] [money] NULL,
      [shippingprice1day] [money] NULL,
      [shippingpricetruck] [money] NULL,
      [shippingpricetruckexpress] [money] NULL,
      [shippingpricetruckwhite] [money] NULL,
      [pyxispackages] [varchar](1000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [sunpackages] [varchar](1000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [ebaydescription] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [pdescription] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [xsell] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]





CREATE TABLE [dbo].[ebaytitles](
      [titleid] [int] IDENTITY(1,1) NOT NULL,
      [title] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [type] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [seller] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
      [packageid] [int] NULL,
      [gallery] [varchar](300) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [price] [money] NOT NULL,
      [webprice] [money] NOT NULL,
      [buyitnowprice] [decimal](7, 2) NULL,
      [startprice] [decimal](7, 2) NULL,
      [oldstartprice] [decimal](7, 2) NULL,
      [itemid] [bigint] NULL,
      [date] [datetime] NULL,
      [oldbuyitnowprice] [decimal](7, 2) NULL,
      [s1] [smallint] NULL,
      [b1] [smallint] NULL,
      [site] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [site2] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [oldstartprice2] [decimal](7, 2) NULL,
      [oldbuyitnowprice2] [decimal](7, 2) NULL,
      [u1] [decimal](7, 2) NULL,
      [ea] [smallint] NULL
) ON [PRIMARY]


want to join sitedata.xsell [text] on ebaytitles.packageid [int]
but sitedata.xsell is numbers separated by commas

so if there are 2 numbers, I want them to be joined 2 times
so if there are 3 numbers, I want them to be joined 3 times
so if there are 4 numbers, I want them to be joined 4 times


only want to do this where sitedata.xsell has values
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 34975835
try
select A.*, B.*
from ebaytitles A
inner join sitedata B on CHARINDEX(CAST(A.packageid as varchar), B.xsell, 1) > 0

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 34975870
create a function like this.
CREATE FUNCTION [dbo].[fn_Split](@text nvarchar(max), @delimiter char(1) = ' ')
RETURNS @Strings TABLE (position int IDENTITY PRIMARY KEY, value nvarchar(max)) AS 
  BEGIN 
DECLARE @index int
    SET @index = -1
  WHILE (LEN(@text) > 0)
  BEGIN
    SET @index = CHARINDEX(@delimiter , @text)
     IF (@index = 0) AND (LEN(@text) > 0)
  BEGIN 
 INSERT INTO @Strings VALUES (@text)
  BREAK 
    END
     IF (@index > 1)
  BEGIN
 INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
    SET @text = RIGHT(@text, (LEN(@text) - @index))
    END 
   ELSE 
    SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
 RETURN
    END
 GO

Open in new window

use this function in your code when joining the tables.
 
select *
   from (select * 
           from sitedata 
          cross apply dbo.fn_Split(xsell,',')) t1
   join ebaytitles t2 on t1.Value = t2.packageid 

Open in new window

0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 34977094
You can recommend to change the table structure to improve the performance. The data in [xsell] column needs to be in a separate table to make the design comply with first form of Normalization. It wil help to add scalability and performance to your database.
0
 

Author Closing Comment

by:rgb192
ID: 34997838
this is the best solution that works
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question