1

I am looking to define data types by fields in SQL Server 2005. My source is an Excel spreadsheet and I cannot define by columns because each row requires a set of definitions applied to each field in that row. Ex, row 1 requires field1 to be CHAR while in row 2 field 1 needs to be DATE. Any suggestions? Thanks in advance.

1
  • 2
    What does "Field" mean in the context you're using it? Are you referring to an Excel "cell"? SQL Server requires that a column be a specific datatype; you can't have one row of the column be a date and the next row of that same column be an integer, unless you convert each of them into a common data type (for example VarChar). Commented May 5, 2011 at 18:07

1 Answer 1

1

You can use a sql_variant column to store any data type. It will allow Excel dates, strings, and numbers to be stored in the same SQL Server column. A sql_variant column can contain a values of any SQL data type with no loss of precision.

CREATE TABLE Sheet
(
    RowNumber int NOT NULL,
    ColumnLetter varchar(5) NOT NULL,
    FieldValue sql_variant NULL,
    PRIMARY KEY (RowNumber, ColumnLetter)
)
INSERT Sheet SELECT 1, 'A', 'Values'
INSERT Sheet SELECT 2, 'A', GETDATE()
INSERT Sheet SELECT 3, 'A', 123
INSERT Sheet SELECT 4, 'A', 123.00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.