Guessing Data Type of columns stored as VarChar in SQLServer

时间:2015-07-28 17:02:53

标签: sql sql-server ssis

I'm just wondering if anyone has created or heard of a function to guess the appropriate data type of data stored in a SQLServer table as VarChar.

I'm working on an SSIS package that you can point at a directory, and it will loop through and create tables / import data for every CSV that exists it. I'm having trouble with specifying the data types before import, so as a work around I would like to import all the data as VarChar(50) into a temporary table and then run some sort of function to analyze each column for the appropriate data type (int, decimal, float, etc) so I can use that to script the create table and insert statements.

So for example I'd like to be able to point a function or query at this temp table

CREATE TABLE [#Data]
(
[ProductCode] varchar(50),
[ProductName] varchar(50),
[Year] varchar(50),
[Total_volume] varchar(50),
[Total_Quantity] varchar(50),
[PercentSold] varchar(50)
)

to read through the data and determine what data type / length is most appropriate - much like the 'Suggest Data Type' tool in Excel Connection Manager does, only something I can tie into a variable to be done dynamically. It should end up looking something like

CREATE TABLE [Data]
(
[ProductCode] varchar(6),
[ProductName] varchar(11),
[Year] int,
[Total_volume] int,
[Total_Quantity] int,
[PercentSold] decimal(3,2)
)

Any thoughts?

Thanks!

0 个答案:

没有答案