| Author |
Topic |
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-10-31 : 13:09:36
|
| I want to extract data from a string into different fields in a table and contained in field called Filetype and table called RECEIVABLE. Here is example Strings contain in "5435 Akwa Joseph Ndifon 200k 34514.pdf""5436 Njang 250k 34516.pdf"Each string should parlsed into the following fields:CustomerID, CustomerName, Amount Paid, and ReceiptNumber 5435 Akwa Joseph Ndifon 200000 34514 5436 Njang 250000 34516please note 200k in the string means 200000and 250K means 250000where K = 1000 ( or replace by 000)What T-SQL command can be used to do this, I will really appreciate any help or direction on thispeter Akwichek |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-10-31 : 13:40:33
|
| declare @s table (s varchar(1000), id int identity)insert @s select '5435 Akwa Joseph Ndifon 200k 34514.pdf'insert @s select '5436 Njang 250k 34516.pdf'select CustomerID, CustomerName, [Amount Paid] = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumberfrom (select CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNumber = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-31 : 13:56:02
|
| you can make use SUBSTRING and PATINDEX functions to get the required parts from the string------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-10-31 : 23:23:28
|
quote: Originally posted by nigelrivett declare @s table (s varchar(1000), id int identity)insert @s select '5435 Akwa Joseph Ndifon 200k 34514.pdf'insert @s select '5436 Njang 250k 34516.pdf'select CustomerID, CustomerName, [Amount Paid] = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumberfrom (select CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNumber = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hello Sir That works. This was really excellent. I thank you a lotHow to modify same query to update two fields in the same table? The fields are Amount and ReceiptNumberThankspeter Akwichek |
 |
|
|
jassi.singh
Posting Yak Master
122 Posts |
Posted - 2011-11-01 : 04:31:02
|
quote: Originally posted by pakwichek
quote: Originally posted by nigelrivett declare @s table (s varchar(1000), id int identity)insert @s select '5435 Akwa Joseph Ndifon 200k 34514.pdf'insert @s select '5436 Njang 250k 34516.pdf'select CustomerID, CustomerName, [Amount Paid] = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumberfrom (select CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNumber = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hello Sir That works. This was really excellent. I thank you a lotHow to modify same query to update two fields in the same table? The fields are Amount and ReceiptNumberThankspeter Akwichek
---------------------------------You can store your result in temp table and them you can do something like belowupdate tablename set field1=new value from tablename inner join temptablenamePlease mark answer as accepted if it helped you.Thanks,Jassi Singh |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-01 : 04:45:11
|
quote: Originally posted by pakwichek
quote: Originally posted by nigelrivett declare @s table (s varchar(1000), id int identity)insert @s select '5435 Akwa Joseph Ndifon 200k 34514.pdf'insert @s select '5436 Njang 250k 34516.pdf'select CustomerID, CustomerName, [Amount Paid] = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumberfrom (select CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNumber = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hello Sir That works. This was really excellent. I thank you a lotHow to modify same query to update two fields in the same table? The fields are Amount and ReceiptNumberThankspeter Akwichek
change select to an updateupdate aset Amount = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumber=ReceiptNumberfrom (select Amount ,ReceiptNumber,CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNo = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select *,s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-01 : 04:55:30
|
quote: Originally posted by jassi.singh
quote: Originally posted by pakwichek
quote: Originally posted by nigelrivett declare @s table (s varchar(1000), id int identity)insert @s select '5435 Akwa Joseph Ndifon 200k 34514.pdf'insert @s select '5436 Njang 250k 34516.pdf'select CustomerID, CustomerName, [Amount Paid] = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumberfrom (select CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNumber = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hello Sir That works. This was really excellent. I thank you a lotHow to modify same query to update two fields in the same table? The fields are Amount and ReceiptNumberThankspeter Akwichek
---------------------------------You can store your result in temp table and them you can do something like belowupdate tablename set field1=new value from tablename inner join temptablenamePlease mark answer as accepted if it helped you.Thanks,Jassi Singh
no need of temporary tablesee my suggestionyou can wrap them inline in update------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 08:31:10
|
quote: Originally posted by visakh16
quote: Originally posted by pakwichek
quote: Originally posted by nigelrivett declare @s table (s varchar(1000), id int identity)insert @s select '5435 Akwa Joseph Ndifon 200k 34514.pdf'insert @s select '5436 Njang 250k 34516.pdf'select CustomerID, CustomerName, [Amount Paid] = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumberfrom (select CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNumber = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hello Sir That works. This was really excellent. I thank you a lotHow to modify same query to update two fields in the same table? The fields are Amount and ReceiptNumberThankspeter Akwichek
change select to an updateupdate aset Amount = case when [Amount Paid] like '%k' then CONVERT(int,replace([Amount Paid],'k','')) * 1000 else CONVERT(int,[Amount Paid]) end, ReceiptNumber=ReceiptNumberfrom (select Amount ,ReceiptNumber,CustomerID = SUBSTRING(s,1,s1),CustomerName = SUBSTRING(s,s1+1,s2-s1), [Amount Paid]= SUBSTRING(s,s2+1,s3-s2), ReceiptNo = SUBSTRING(s,s3+1,len(s)-s3-4), *from (select *, s2 = len(s) - CHARINDEX(' ',REVERSE(s),LEN(s)-s3+1)from (select *,s, s1 = charindex(' ',s), s3 = len(s) - CHARINDEX(' ',reverse(s))from @s) a) a) a------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Here is the Table Structure I am using to do this update. ID intCUSTOMERID intFileName varchar(200)FileType varchar(30)CustomerName Varchar(60)TITReceipt intAmount intI updated the query based on the above table structure as below-----update aset Amount = case when [Amount] like '%k' then CONVERT(int,replace([Amount],'k','')) * 1000 else CONVERT(int,[Amount]) end, TITReceipt=TITReceipt,CustomerName= CustomerName from (select Amount ,TITReceipt ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName = SUBSTRING([FileName] ,s1+1,s2-s1), [Amount]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt = SUBSTRING([FileName] ,s3+1,len(s)-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *,[FileName] , s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'----But When I execute the above, I get the following error"Server: Msg 8156, Level 16, State 1, Line 1The column 'FileName' was specified multiple times for 'a'."Can't quite figure this error out, I am sure, one more correction we are done, I really appreciate all your helpthankspeter Akwichek |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-01 : 11:43:39
|
| [code]update aset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select Amount ,TITReceipt ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len(s)-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-01 : 13:05:40
|
| This gets rid of the s error but I still don't think it will work as you need to update the table rather than the derived info here.I think it needs to join to STUDENT_DIR_INFORupdate STUDENT_DIR_INFORset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select Amount ,TITReceipt ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 13:16:04
|
quote: Originally posted by nigelrivett This gets rid of the s error but I still don't think it will work as you need to update the table rather than the derived info here.I think it needs to join to STUDENT_DIR_INFORupdate STUDENT_DIR_INFORset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select Amount ,TITReceipt ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
OK, SIr, I executed the script but has another error below"Server: Msg 207, Level 16, State 3, Line 31Invalid column name 's'.Server: Msg 8156, Level 16, State 1, Line 31The column 'Amount' was specified multiple times for 'a'."Here is more information with table and data to help with error fixing:-----------DECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update aset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select Amount ,TITReceipt ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len(s)-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'peter Akwichek |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-01 : 13:28:37
|
| A number of issue fixedDECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update aset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select Amount ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1),[AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4)from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'select * from @Student_Dir_Infor==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-01 : 13:30:15
|
| [code]DECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update aset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select CustomerIDVal = SUBSTRING([FileName] ,1,s1),CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'select * from @Student_Dir_Inforoutput------------------------------------------ID CUSTOMERID FileName FileType CustomerName TITReceipt Amount1 5261 5261 ROkara 15k 3996.pdf BANK RECEIPT ROkara 3996 150002 5266 5266 MNdifon 15k 10887.pdf BANK RECEIPT MNdifon 10887 150003 5267 5267 ADoh 15k 10886.pdf BANK RECEIPT ADoh 10886 150004 5268 5268 MMbonda 200k 10888.pdf BANK RECEIPT MMbonda 10888 2000005 5269 5269 MOkpara 100k 10889.pdf BANK RECEIPT MOkpara 10889 100000[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 13:50:51
|
quote: Originally posted by visakh16
DECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update aset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select CustomerIDVal = SUBSTRING([FileName] ,1,s1),CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'select * from @Student_Dir_Inforoutput------------------------------------------ID CUSTOMERID FileName FileType CustomerName TITReceipt Amount1 5261 5261 ROkara 15k 3996.pdf BANK RECEIPT ROkara 3996 150002 5266 5266 MNdifon 15k 10887.pdf BANK RECEIPT MNdifon 10887 150003 5267 5267 ADoh 15k 10886.pdf BANK RECEIPT ADoh 10886 150004 5268 5268 MMbonda 200k 10888.pdf BANK RECEIPT MMbonda 10888 2000005 5269 5269 MOkpara 100k 10889.pdf BANK RECEIPT MOkpara 10889 100000------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Oh No, what did I do wrong. I can't get the same result postedI get this errorServer: Msg 4421, Level 16, State 1, Line 31Derived table 'a' is not updatable because a column of the derived table is derived or constant.I also try running it by exactly and changing change to the true underline sql table,, same error. I hope I did not copy wrong code accidentally, I am so sorry,.. Looks like it should work!! peter Akwichek |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-01 : 13:51:36
|
| Try my version.What version of sql server are you using - you might be back to my earlier suggestion.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-01 : 13:59:07
|
| Try this oneand I've just amended it to add all of the columns to the join.DECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update @Student_Dir_Inforset Amount = case when a.[AmountVal] like '%k' then CONVERT(int,replace(a.[AmountVal],'k','')) * 1000 else CONVERT(int,a.[AmountVal]) end, TITReceipt=a.TITReceiptVal,CustomerName= a.CustomerNameVal from (select Amount ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1),[AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,FileName ,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4)from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) ajoin @Student_Dir_Infor sdion a.CUSTOMERID = sdi.CUSTOMERIDWHERE sdi.FileType='BANK RECEIPT'and sdi.FileName = a.FileNameand sdi.FileType = a.FileTypeselect * from @Student_Dir_Infor==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 13:59:57
|
quote: Originally posted by nigelrivett Try my version.What version of sql server are you using - you might be back to my earlier suggestion.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
I have SQL 2000peter Akwichek |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-01 : 14:02:07
|
| Could be the issue - try that last one I posted.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 14:11:05
|
quote: Originally posted by nigelrivett Try this oneand I've just amended it to add all of the columns to the join.DECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update @Student_Dir_Inforset Amount = case when a.[AmountVal] like '%k' then CONVERT(int,replace(a.[AmountVal],'k','')) * 1000 else CONVERT(int,a.[AmountVal]) end, TITReceipt=a.TITReceiptVal,CustomerName= a.CustomerNameVal from (select Amount ,CustomerID = SUBSTRING([FileName] ,1,s1),CustomerName ,CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1),[AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceipt,FileType,FileName ,TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4)from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) ajoin @Student_Dir_Infor sdion a.CUSTOMERID = sdi.CUSTOMERIDWHERE sdi.FileType='BANK RECEIPT'and sdi.FileName = a.FileNameand sdi.FileType = a.FileTypeselect * from @Student_Dir_Infor==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
COOOL COOL!! THE JOIN DID THE TRICK!! BRAVO!!THANKS A MILLIONS!! WORKS LIKE A CHARM peter Akwichek |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 14:15:20
|
quote: Originally posted by visakh16
DECLARE @Student_Dir_Infor Table (ID int identity,CUSTOMERID int,FileName varchar(200),FileType varchar(30),CustomerName Varchar(60),TITReceipt int,Amount int)INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5261', '5261 ROkara 15k 3996.pdf', 'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5266', '5266 MNdifon 15k 10887.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5267', '5267 ADoh 15k 10886.pdf' ,'BANK RECEIPT'INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5268', '5268 MMbonda 200k 10888.pdf' ,'BANK RECEIPT' INSERT INTO @Student_Dir_Infor (CUSTOMERID,FileName,FileType)SELECT '5269', '5269 MOkpara 100k 10889.pdf' ,'BANK RECEIPT'update aset Amount = case when [AmountVal] like '%k' then CONVERT(int,replace([AmountVal],'k','')) * 1000 else CONVERT(int,[AmountVal]) end, TITReceipt=TITReceiptVal,CustomerName= CustomerNameVal from (select CustomerIDVal = SUBSTRING([FileName] ,1,s1),CustomerNameVal = SUBSTRING([FileName] ,s1+1,s2-s1), [AmountVal]= SUBSTRING([FileName] ,s2+1,s3-s2), TITReceiptVal = SUBSTRING([FileName] ,s3+1,len([FileName])-s3-4), *from (select *, s2 = len([FileName] ) - CHARINDEX(' ',REVERSE([FileName] ),LEN([FileName] )-s3+1)from (select *, s1 = charindex(' ',[FileName] ), s3 = len([FileName] ) - CHARINDEX(' ',reverse([FileName] ))from @STUDENT_DIR_INFOR) a) a) aWHERE FileType='BANK RECEIPT'select * from @Student_Dir_Inforoutput------------------------------------------ID CUSTOMERID FileName FileType CustomerName TITReceipt Amount1 5261 5261 ROkara 15k 3996.pdf BANK RECEIPT ROkara 3996 150002 5266 5266 MNdifon 15k 10887.pdf BANK RECEIPT MNdifon 10887 150003 5267 5267 ADoh 15k 10886.pdf BANK RECEIPT ADoh 10886 150004 5268 5268 MMbonda 200k 10888.pdf BANK RECEIPT MMbonda 10888 2000005 5269 5269 MOkpara 100k 10889.pdf BANK RECEIPT MOkpara 10889 100000------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
: THANKS A LOT FOR ALL HELP WITH THIS peter Akwichek |
 |
|
|
pakwichek
Starting Member
12 Posts |
Posted - 2011-11-01 : 15:30:51
|
quote: Originally posted by nigelrivett Could be the issue - try that last one I posted.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
Hello Sir, Now that everything works, how to ignore rows that have data conversion errors! I will like to update everything and ignore all errorsThanks a lot for any tip or code snippetpeter Akwichek |
 |
|
|
Next Page
|
|
|