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 8109899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:34:01+00:00 2026-06-06T01:34:01+00:00

Is there a quick way to get a string containing the sql datatype required

  • 0

Is there a quick way to get a string containing the sql datatype required for a column declaration, based on column/column information in the database.

For example I’d want strings returned such as:

varchar(200)
datetime
numeric(35,5)

Obviously I can get the information required to generate this myself from SYS.COLUMNS or from INFORMATION_SCHEMA.COLUMNS and start handling those, but I wanted a (preferably MS provided) way to turn the datatype/maxlength/precision/scale information into the column declaration data type automatically.

I would guess that if there is a standard way to do this it would handle all possible data types, which would be a pain to try to cover manually.

EDIT: Sorry – It seems I’ve not been clear enough on what I want.

As an example, when you script a table for CREATE in SSMS, the resulting script contains the datatypes in the formats I want. Is there a way to get these automatically?

EDIT:

OK: One more go:

What I want is a bit like this question:

Declaring variable type based on a column type

The difference is that I don’t mind having to declare the variable dynamically as I’m working with dynamic SQL already.

  • 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-06-06T01:34:03+00:00Added an answer on June 6, 2026 at 1:34 am

    I’m afraid I don’t know of a proper way, but I’ve had a go at doing the INFORMATION_SCHEMA.Columns way. This is not pretty, but does handle database defaults, nullables, etc.

    SELECT column_name + ' ' + DATA_TYPE + COALESCE('(' + CASE
                                                            WHEN DATA_TYPE = 'XML' THEN NULL
                                                            WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN 'max'
                                                            ELSE Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5))
                                                          END + ')', '(' + Cast(NUMERIC_PRECISION AS NVARCHAR(5)) + ',' + Cast(NUMERIC_SCALE AS NVARCHAR(5)) + ')', '') + ' ' + CASE IS_NULLABLE
                                                                                                                                                                                  WHEN 'YES' THEN 'NULL'
                                                                                                                                                                                  ELSE 'NOT NULL'
                                                                                                                                                                                END + COALESCE(' DEFAULT' + COLUMN_DEFAULT, '')
    FROM   INFORMATION_SCHEMA.Columns
    WHERE  table_name = 'mytable'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there quick way to get the ImageFormat object associated to a particular file
Is there a quick way to get a flattened List<TElement> from an ILookup<TKey, TElement>
I was wondering if there was a quick way to get all of an
Is there a quick way ( existing method) Concatenate array element into string with
Is there any quick way I can get the number of strings within a
I'm wondering if there is a quick/clean way to get the symmetric difference between
Is there a quick way to get the filename and last folder from a
Is there an accepted idiom or a quick and simple way to get a
I need a quick easy way to get a string from a file in
Is there a quick way or function that would tell me true/false if all

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.