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.
Author |
Topic |
noblemfd
Starting Member
38 Posts |
Posted - 2012-02-07 : 16:52:44
|
I WANT TO USE MSSQL 2000 USER-DEFINED FUNCTION TO ACHIEVE THE GOAL BELOW. PLEASE HELP ME OUR, OR IS THERE ANY OTHER METHOD USING MSSQL 2000.PRESENT STATEINVNUM RECEIPTNUM DATE AMOUNT1 000001/000002 11/02/2010 12,222.00/32,234.002 3 000003 21/09/2011 32,345.00EXPECTED RESULTINVNUM RECEIPTNUM DATE AMOUNT1 000001 11/02/2010 12,222.001 000002 11/02/2010 32,234.002 3 000003 21/09/2011 32,345.00Please kindly assist. Or do you know how I can use MSSQL FUNCTION to resolve it? I need it urgently.Thanks |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-07 : 17:13:11
|
How many "/" can INVNUM or AMOUNT have?The REAL Problem is how do you know o "tie" The data together?Just because the 1st occurence all belong together?SELECT *FROM yourTable DJOIN (SELECT DATE, dbo.udf_Table_LineNum( INVNUM , '/') FROM yourTable) AS ION D.DATE = I.DATEJOIN (SELECT DATE, dbo.udf_Table_LineNum( AMOUNT , '/') FROM yourTable) AS AON D.DATE = A.DATEThis isn't correct..but's in the right directionYou really need a keyPlay with this udfUSE [myActions]GO/****** Object: UserDefinedFunction [dbo].[udf_Table_LineNum] Script Date: 02/07/2012 17:06:38 ******/IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[udf_Table_LineNum]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))DROP FUNCTION [dbo].[udf_Table_LineNum]GO/****** Object: UserDefinedFunction [dbo].[udf_Table_LineNum] Script Date: 02/07/2012 17:01:28 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[udf_Table_LineNum](@ParmList varchar(8000), @Delim varchar(20))RETURNS @table TABLE (LineNum int, Parameter varchar(255))AS /* SELECT * FROM dbo.udf_Table_LineNum( 'a|~|b|~|c', '|~|') SELECT * FROM dbo.udf_Table_LineNum( 'a', '|~|') */BEGIN DECLARE @x int, @Parameter varchar(255) SET @x = 1 WHILE CHARINDEX(@Delim, @ParmList)-1 > 0 BEGIN INSERT INTO @table(LineNum, Parameter) SELECT @x, SUBSTRING(@ParmList,1,CHARINDEX(@Delim, @ParmList)-1) SELECT @ParmList = SUBSTRING(@ParmList,CHARINDEX(@Delim, @ParmList)+LEN(@Delim), LEN(@ParmList)-CHARINDEX(@Delim,@ParmList)) SELECT @x = @x + 1 END INSERT INTO @table(LineNum, Parameter) SELECT @x, @ParmList RETURNENDGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
|
|
noblemfd
Starting Member
38 Posts |
Posted - 2012-02-08 : 03:54:01
|
quote: Originally posted by noblemfd I WANT TO USE MSSQL 2000 USER-DEFINED FUNCTION TO ACHIEVE THE GOAL BELOW. PLEASE HELP ME OUR, OR IS THERE ANY OTHER METHOD USING MSSQL 2000.PRESENT STATEINVNUM RECEIPTNUM DATE AMOUNT1 000001/000002 11/02/2010 12,222.00/32,234.002 3 000003 21/09/2011 32,345.00EXPECTED RESULTINVNUM RECEIPTNUM DATE AMOUNT1 000001 11/02/2010 12,222.001 000002 11/02/2010 32,234.002 3 000003 21/09/2011 32,345.00Please kindly assist. Or do you know how I can use MSSQL FUNCTION to resolve it? I need it urgently.Thanks
Thanks so much for your response. The RECEIPTNUM AND AMOUNT can have one, two or more "/", also some may not have "/" at all, and some may be null. Let me explain"1. table name invoice have four fields.2. "/" signifies the customers with multiple receipts3. i want to split the TABLE and remove the "/" where the fileds RECEIPTNUM and AMOUNT are having "/" using function or other methods. |
|
|
noblemfd
Starting Member
38 Posts |
Posted - 2012-03-21 : 17:49:07
|
It can have between zero and four "/"quote: Originally posted by X002548 How many "/" can INVNUM or AMOUNT have?The REAL Problem is how do you know o "tie" The data together?Just because the 1st occurence all belong together?SELECT *FROM yourTable DJOIN (SELECT DATE, dbo.udf_Table_LineNum( INVNUM , '/') FROM yourTable) AS ION D.DATE = I.DATEJOIN (SELECT DATE, dbo.udf_Table_LineNum( AMOUNT , '/') FROM yourTable) AS AON D.DATE = A.DATEThis isn't correct..but's in the right directionYou really need a keyPlay with this udfUSE [myActions]GO/****** Object: UserDefinedFunction [dbo].[udf_Table_LineNum] Script Date: 02/07/2012 17:06:38 ******/IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[udf_Table_LineNum]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))DROP FUNCTION [dbo].[udf_Table_LineNum]GO/****** Object: UserDefinedFunction [dbo].[udf_Table_LineNum] Script Date: 02/07/2012 17:01:28 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[udf_Table_LineNum](@ParmList varchar(8000), @Delim varchar(20))RETURNS @table TABLE (LineNum int, Parameter varchar(255))AS /* SELECT * FROM dbo.udf_Table_LineNum( 'a|~|b|~|c', '|~|') SELECT * FROM dbo.udf_Table_LineNum( 'a', '|~|') */BEGIN DECLARE @x int, @Parameter varchar(255) SET @x = 1 WHILE CHARINDEX(@Delim, @ParmList)-1 > 0 BEGIN INSERT INTO @table(LineNum, Parameter) SELECT @x, SUBSTRING(@ParmList,1,CHARINDEX(@Delim, @ParmList)-1) SELECT @ParmList = SUBSTRING(@ParmList,CHARINDEX(@Delim, @ParmList)+LEN(@Delim), LEN(@ParmList)-CHARINDEX(@Delim,@ParmList)) SELECT @x = @x + 1 END INSERT INTO @table(LineNum, Parameter) SELECT @x, @ParmList RETURNENDGO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
|
|
|
|
|
|
|
|