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
 Import/Export (DTS) and Replication (2000)
 incorrect column format from excel

Author  Topic 

qwertyjjj
Posting Yak Master

131 Posts

Posted - 2008-04-30 : 11:14:36
Hi
I run this code to import data from an Excel file on the server.
It doesn't error but there is a bug in importing data from a Text datatype column in Excel.
The text is in the format 900012345
In SQL the datatype is nvarchar 510
But during the import it is somehow converted to 9.11343e+007

Is there anyway to check that the Excel file has the correct format before importing?
Or is there something wrong with the datatype I am using?


ALTER PROCEDURE kpi_UploadData

AS

BEGIN TRANSACTION

SELECT * INTO #zarinvreg
FROM OpenDataSource(
'Microsoft.Jet.OLEDB.4.0','Data Source="c:\KPIUpload\Data.xls";Extended properties=Excel 8.0')...zarinvreg_CURR$
IF @@ERROR <> 0 BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back'
RETURN
END

SELECT * INTO #zarageddebt
FROM OpenDataSource(
'Microsoft.Jet.OLEDB.4.0','Data Source="c:\KPIUpload\Data.xls";Extended properties=Excel 8.0')...zarageddebt_CURR$
IF @@ERROR <> 0 BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back'
RETURN
END

SELECT * INTO #baddebt
FROM OpenDataSource(
'Microsoft.Jet.OLEDB.4.0','Data Source="c:\KPIUpload\Data.xls";Extended properties=Excel 8.0')...baddebt_CURR$
IF @@ERROR <> 0 BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back'
RETURN
END

DECLARE @currentmonth varchar(20), @currentyear varchar(4)
set @currentmonth = datename(mm,dateadd(mm,-1,getdate())) --because they always run this on the 6th working day of month
--Therefore run in December is November's data
set @currentyear = datepart(yyyy,dateadd(mm,-1,getdate()))

INSERT INTO MonthData (MonthDataInt, MonthName, [Year], Published)
VALUES (1, @currentmonth, @currentyear, 0) --it is updated to current month, i.e. month 0, when data is published. In meantime all tables have MonthData = 1

INSERT INTO zarinvreg ([Profit ctr], Customer, [Name 1], Docno, [Year], Itm, Type, [Inv ref], [Sales doc], Curr, [Clearing Doc], CoCd, [Doc date],
[Amtin loccur], [Count],
[Clearing Date], [LC2 amount], MonthData)
SELECT [Profit ctr], Customer, [Name 1], [Doc#no#], [Year], Itm, Type, [Inv# ref#], [Sales doc#], [Curr#], [Clearing Doc], CoCd, [Doc# date],
[Amt#in loc#cur#], [Count],
[Clearing Date], [LC2 amount], 1 AS MonthData
FROM #zarinvreg
IF @@ERROR <> 0 BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back'
RETURN
END

--update division codes?
INSERT INTO zarageddebt (Branch, Customer, [Name 1], [Profit ctr], [Inv ref], Docno, Type, [Project Desc], [Sales doc], CoCd, [Doc date],
Total, [1To90], [91To120], [121To150],
[151To180], [181To365], MoreThan366, MonthData)
SELECT Branch, Customer, [Name 1], [Profit ctr], [Inv# ref#], [Doc#no#], Type, [Project Desc], [Sales doc#], CoCd, [Doc# date],
Total, [ 1 - 90], [91 - 120], [121 - 150],
[151 - 180], [181 - 365], [>= 366], 1 AS MonthData
FROM #zarageddebt
IF @@ERROR <> 0 BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back'
RETURN
END

--update division codes?
INSERT INTO baddebt ( CoCd, Customer, [Name 1], [Profit ctr], Docno, [Sales doc], [Project Desc], [Doc date], Total, Nett, [Bad Debt], MonthData)
SELECT CoCd, Customer, [Name 1], [Profit ctr], [Doc#no#], [Sales doc#], [Project Desc], [Doc# date], Total, Nett, [O/S Bad Debt], 1 AS MonthData
FROM #baddebt
IF @@ERROR <> 0 BEGIN
ROLLBACK TRANSACTION
PRINT 'Rolled back'
RETURN
END

--update division codes?

DROP TABLE #zarinvreg
DROP TABLE #zarageddebt
DROP TABLE #baddebt

EXEC bonus_DataCorrection
EXEC kpi_UpdateData

COMMIT TRANSACTION

GO

qwertyjjj
Posting Yak Master

131 Posts

Posted - 2008-05-01 : 05:34:40
any ideas?
Go to Top of Page

qwertyjjj
Posting Yak Master

131 Posts

Posted - 2008-05-01 : 05:48:08
Found it. I used this:
CONVERT(varchar(20), CAST(doc#no# AS decimal(38,0)))
Go to Top of Page
   

- Advertisement -