Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SPLIT FUNCTION

Author  Topic 

noblemfd
Starting Member

38 Posts

Posted - 2012-02-20 : 16:10:34
Am developing an application using vb6 and mssql 2000. I want to split some of the columns in the table. The delimiter is “/”, and it appears once or twice or three times or more or not at all in some of the columns. See the illustration below.

BEFORE

InvID | ReceiptNo | Amount | Date
1 | 00001/00002 | 25,000/34,000 | 12-11-2011
2 | | |
3 | 00003 | 24,000 | 13-11-2011
4 | 00004/00005/00006 | 12,231/21,211/555 | 14-11-2011

AFTER (EXPECTED RESULT

InvID | ReceiptNo | Amount | Date
1 | 00001 | 25,000 | 12-11-2011
1 | 00002 | 34,000 | 12-11-2011
2 | | |
3 | 00003 | 24,000 | 13-11-2011
4 | 00004 | 12,231 | 14-11-2011
4 | 00005 | 21,211 | 14-11-2011
4 | 00006 | 555 | 14-11-2011

Please i need it urgently
Please i need it urgently

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-20 : 16:29:01
why are storing values like this?
have you heard about normalisation and 1 st normal form?
storing like this will really complicate matters

in sql 2000 there's no easy way of doing this other than looping logic

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

noblemfd
Starting Member

38 Posts

Posted - 2012-03-02 : 18:17:37
I know normalization, but the table has been designed before I got there and I need to resolve it. Please help me out

quote:
Originally posted by visakh16

why are storing values like this?
have you heard about normalisation and 1 st normal form?
storing like this will really complicate matters

in sql 2000 there's no easy way of doing this other than looping logic

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-03-03 : 04:03:18
How many split values is there at most in any single column?


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

noblemfd
Starting Member

38 Posts

Posted - 2012-03-03 : 14:18:13

At most there are 4 splits in any of the single columns, while some don't even have splits at all. As shown in the sample. Thanks

quote:
Originally posted by SwePeso

How many split values is there at most in any single column?


N 56°04'39.26"
E 12°55'05.63"


Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-03-03 : 17:16:46
quote:
Originally posted by visakh16

in sql 2000 there's no easy way of doing this other than looping logic

Yes.
DECLARE	@Sample TABLE
(
InvID INT,
ReceiptNo VARCHAR(1000),
Amount VARCHAR(1000),
[Date] DATETIME
)

SET DATEFORMAT DMY

INSERT @Sample
SELECT 1, '00001/00002', '25,000/34,000', '12-11-2011' UNION ALL
SELECT 2, NULL, NULL, NULL UNION ALL
SELECT 3, '00003', '24,000', '13-11-2011' UNION ALL
SELECT 4, '00004/00005/00006', '12,231/21,211/555', '14-11-2011'

-- SwePeso
SELECT InvID,
ReceiptNo,
Amount,
[Date]
FROM (
SELECT InvID,
PARSENAME(REPLACE(ReceiptNo, '/', '.'), 4) AS ReceiptNo,
PARSENAME(REPLACE(Amount, '/', '.'), 4) AS Amount,
[Date]
FROM @Sample

UNION

SELECT InvID,
PARSENAME(REPLACE(ReceiptNo, '/', '.'), 3) AS ReceiptNo,
PARSENAME(REPLACE(Amount, '/', '.'), 3) AS Amount,
[Date]
FROM @Sample

UNION

SELECT InvID,
PARSENAME(REPLACE(ReceiptNo, '/', '.'), 2) AS ReceiptNo,
PARSENAME(REPLACE(Amount, '/', '.'), 2) AS Amount,
[Date]
FROM @Sample

UNION

SELECT InvID,
PARSENAME(REPLACE(ReceiptNo, '/', '.'), 1) AS ReceiptNo,
PARSENAME(REPLACE(Amount, '/', '.'), 1) AS Amount,
[Date]
FROM @Sample
) AS d
WHERE ReceiptNo IS NOT NULL AND Amount IS NOT NULL AND [Date] IS NOT NULL
OR ReceiptNo IS NULL AND Amount IS NULL AND [Date] IS NULL



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -