SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SPLIT FUNCTION
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

noblemfd
Starting Member

Nigeria
18 Posts

Posted - 02/20/2012 :  16:10:34  Show Profile  Reply with Quote
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

India
47188 Posts

Posted - 02/20/2012 :  16:29:01  Show Profile  Reply with Quote
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

Nigeria
18 Posts

Posted - 03/02/2012 :  18:17:37  Show Profile  Reply with Quote
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

Sweden
29138 Posts

Posted - 03/03/2012 :  04:03:18  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

Nigeria
18 Posts

Posted - 03/03/2012 :  14:18:13  Show Profile  Reply with Quote

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

Sweden
29138 Posts

Posted - 03/03/2012 :  17:16:46  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000