change datatype in a table

Rock Hitman 46 Reputation points
2020-11-02T20:24:12.783+00:00

Hi, I want to change dataType from int to varchar for columns MakeID,ModelID.vahicleTypeID
Reason is sometimes we get the data as string.

As this table is already in Production, we just need to modify the datatype of the columns. How easy is this doing without impacting much. Please advise

CREATE TABLE [dbo].[TestData](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MakeID] [int] NOT NULL,
[ModelID] [int] NOT NULL,
[Model] varchar NOT NULL,
[VehicleTypeID] [int] NOT NULL,
[VehicleType] varchar NOT NULL,
[Year] [int] NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[UpdatedDate] [datetime] NULL,
CONSTRAINT [PK_TestData] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

GO

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

2 answers

Sort by: Most helpful
  1. Tom Phillips 17,731 Reputation points
    2020-11-02T21:24:13.187+00:00

    Please see:

    https://video2.skills-academy.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-ver15

    ALTER TABLE  [dbo].[TestData] ALTER COLUMN [MakeID] varchar(100) NOT NULL;  
    
    1 person found this answer helpful.

  2. EchoLiu-MSFT 14,581 Reputation points
    2020-11-03T02:48:44.027+00:00

    If you are not sure whether the modification is correct, you can check the final data type with the following code:

     select sql_variant_property(columnname,basetype) from tablename   
    

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.
    Hot issues October--https://video2.skills-academy.com/en-us/answers/questions/142427/users-always-get-connection-timeout-problem-when-u.html

    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.