Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 333513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:58:06+00:00 2026-05-12T09:58:06+00:00

I have a smalldatetime column that I need to alter to be a datetime

  • 0

I have a smalldatetime column that I need to alter to be a datetime column. This is something that will be part of an install process, so it cannot be a manual procedure. Unfortunately, the column has a few indexes and a not null constraint on it. The indexes are performance related and would need to be retained only using the new data type. Is it possible to write a statement that will allow me to retain the relevant information while still altering the column datatype? If so, how can this be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:58:06+00:00Added an answer on May 12, 2026 at 9:58 am

    You can not change the datatype from smalldatetime to datetime with the indexes, unique constraints, foreign key constraints or check constraints in place. You will have to drop them all prior to changing the type. Then:

    alter table T alter column TestDate datetime not null
    

    Then recreate the constraints and indexes that still apply.


    Some different approaches for generating the drop and creates:

    1) If you have given explicit names to all indexes and constraints then your installer can run a static script in each environment (dev, test, user acceptance testing, performance testing, etc, production.)

    To generate this explicit script you can:
    a) Use SSMS (or with SQL Server 2000, enterprise manager) to script the create and drop statements.
    b) Work from you source code repository to discover the names and definitions of the dependent objects and put together the appropriate static script.
    c) Attempt to run the alter statement. See what it fails on. Look up the definitions and hand write the drop and create. (Personally, this would be great for writing the drop, not so good at the create.)

    2) If you have not given explicit names to all indexes and constraints, then your installer will have to query the data dictionary for the appropriate names and use dynamic SQL to run the drops, in the correct order, prior to the alter column statement and then the creates, in the correct order, after the alter column.

    This will be simpler if you know that there are no constraints, and just indexes.

    There may be tools or libraries that already know how to do this.

    Also, if this is a packaged application, you may not be assured that the local DBAs have not added indexes.

    NOTE: If there is a unique constraint, it will have built an index, which you will not be able to drop with DROP INDEX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the field: APP_DATE (smalldatetime) I'm doing this query: INSERT INTO table (TYPE,
I have a column of smalldatetime type, date I'd like to run a query
I have a table with 800+ records. In this table I have a column
I have realized that a smalldatetime sql server data type has been mapped to
I have two tables in a legacy database that I need to map using
I have a table that has a smalldatetime NOT NULL field with a default
I have a query like this: SELECT 'Last 7 Days' AS Date_Range, CONVERT(smalldatetime, GETDATE())
I have data that on the server is in the form of a smalldatetime
I have stored procedure that returns results sorted dynamically. The parent folder (this is
I have a table [Users] with the following columns: INT SmallDateTime Bit Bit [UserId],

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.