473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

basic sql query needed

Ok, I'm a beginner so forgive me for my ignorance. Could someone help
me with this?

tbl_x has two fields xid and xlist
xlist being a list of numbers.....1, 5, 6, 8

i want to create a new table from tbl_x that converts each number in
that xlist into a row in tbl_new

so for example
tbl_x has 3 rows
xid xlist
1 1, 4, 5
2 2, 3, 7
3 2, 1, 7

i need a query or sql script that will convert that table with a list
into the following....

tbl_new
id xid xlid
1 1 1
2 1 4
3 1 5
4 2 2
5 2 3
6 2 7
7 3 2
8 3 1
9 3 7
Jul 20 '05 #1
2 1885
is xlist always 7 char width? or will it have some thing like 123,1,333

re****@freeshell.org (G Hopper) wrote in message news:<15**************************@posting.google. com>...
Ok, I'm a beginner so forgive me for my ignorance. Could someone help
me with this?

tbl_x has two fields xid and xlist
xlist being a list of numbers.....1, 5, 6, 8

i want to create a new table from tbl_x that converts each number in
that xlist into a row in tbl_new

so for example
tbl_x has 3 rows
xid xlist
1 1, 4, 5
2 2, 3, 7
3 2, 1, 7

i need a query or sql script that will convert that table with a list
into the following....

tbl_new
id xid xlid
1 1 1
2 1 4
3 1 5
4 2 2
5 2 3
6 2 7
7 3 2
8 3 1
9 3 7

Jul 20 '05 #2
re****@freeshell.org (G Hopper) wrote in message news:<15**************************@posting.google. com>...
Ok, I'm a beginner so forgive me for my ignorance. Could someone help
me with this?

tbl_x has two fields xid and xlist
xlist being a list of numbers.....1, 5, 6, 8

i want to create a new table from tbl_x that converts each number in
that xlist into a row in tbl_new

so for example
tbl_x has 3 rows
xid xlist
1 1, 4, 5
2 2, 3, 7
3 2, 1, 7

i need a query or sql script that will convert that table with a list
into the following....

tbl_new
id xid xlid
1 1 1
2 1 4
3 1 5
4 2 2
5 2 3
6 2 7
7 3 2
8 3 1
9 3 7


It's not pretty but, this code works. I normally avoid cursors, but
couldn't get the function to work as a subquery in the from clause.
It should get you started.

create table t_ids
(xid int, xlist varchar(25))
go

create table t_ids2
(xid int, xl int)
go

insert into t_ids
select 1 as xid, '1, 4, 5'
union select 2, '2, 3, 7'
union select 3, '2, 1, 7'

GO

CREATE FUNCTION f_split(@id int, @str varchar(255), @sep char(1))
RETURNS @vals table (id int, val varchar(25))
as
begin
declare @start int,
@found int
set @start = 1
set @found = 0
while (@start > 0) begin
set @found = charindex(@sep, @str, @start)
if @found = 0 begin
if @start > 1
insert into @vals values(@id, ltrim(rtrim(substring(@str, @start,
len(@str) - @start + 1))))
break
end
else begin
insert into @vals values(@id, ltrim(rtrim(substring(@str, @start,
@found - @start))))
set @start = @found + 1
end
end

return
end
GO


DECLARE @id as int, @xl as varchar(25)

DECLARE c_xl CURSOR FAST_FORWARD
FOR SELECT xid, xlist FROM t_ids
open c_xl
FETCH c_xl into @id, @xl
WHILE @@Fetch_Status = 0 BEGIN
insert into t_ids2
select * from dbo.f_split(@id, @xl, ',')
FETCH c_xl into @id, @xl

END
CLOSE c_xl
select * from t_ids2
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ryan Gaffuri | last post by:
I know that this works. I just don't get the syntax. I know its checking the OS. just not sure how it works. var v = navigator.appVersion.toUpperCase() if (1+v.indexOf('WIN98') os =...
2
by: John | last post by:
I'm a bit confused on how to use the query builder to create an expression that will calculate gross revenue per gross unit using the tables below: table_1: Product PLtype (e.g., "Gross Units"...
1
by: jm | last post by:
I manually put a datagrid in on a form. I then added this code to an event to populate it: OdbcConnection conn = new OdbcConnection("DSN=CardBase"); sqlCardHolders = "SELECT * FROM...
2
by: Fay Yocum | last post by:
BEWARE beginner questions!! I have some experience in Access but never as much as I want or need. I have decided to get in on VB.Net. I would only rate myself in Access as a...
4
by: CSharpguy | last post by:
I'm not sure if this is the correct forum or not, but I have a basic question. Currently we have are doing calculations via stored procedures and then returning the results back to the client in...
6
by: JimmyKoolPantz | last post by:
I have been given the task of converting a program from VFP (visual foxpro) to Visual Basic.net. My question is "Is it possible to generate a DBF file Dynamically(at runtime) using Visual...
2
by: aashirwaads | last post by:
Hi Friends, I really need help from you people...I am a very new user of Oracle database...so need some basic tips from you... 1.What query do u type in sql*plus to see all the tables in a...
6
by: BJMurphy | last post by:
Hi All, I am used to other SQL engines, and have a few basic questions-- 1)If I wanted to conditionally drop a table, does SQL Server have a way to natively do this? Many SQL implementations...
1
by: Coll | last post by:
I'm working on a database that someone else created. I have a form - frm_main that is based on a query that is based on two tables. The join is such that the first table tbl_JCN has all records...
6
by: Vince | last post by:
Hello all, I am using Visual Basic to open a saved query and then save information in the query to an array for later use. The problem is that the same query shows different results when opened...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.