Sql server how to change column type from int to date

Shannon Shen 0 Reputation points
2024-12-18T20:15:02.31+00:00

Sql server how to change column type from int to date

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
108 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 114.6K Reputation points MVP
    2024-12-18T22:22:03.6+00:00

    You would need to do one of:

    1. Create a new table with the desired schema and copy data over.
    2. Add a new column, and copy data over, and then drop the old column.

    In either case, you need to know how to convert your current values to date. That is, you need to know which rules to apply.

    You cannot use ALTER TABLE, as testified by this example:

    CREATE TABLE nisse (a int NOT NULL)
    go
    ALTER TABLE nisse ALTER COLUMN a date NOT NULL
    

    The error message is:

    Msg 206, Level 16, State 2, Line 3 Operand type clash: int is incompatible with date

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.