Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get correct datatype names for numeric and date type values


How to get correct datatype names for numeric and date type values

Author
Message
toravi.pentaho
toravi.pentaho
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 5
Hi,

When i try the below sql.I'm getting proper result for Varchar datatype
field columns,BUT FOR NUMERIC AND DATE type coulmn im getting null in data_type column.Can you please check this,


SELECT column_name AS [Name],
IS_NULLABLE AS [Null?],
DATA_TYPE + '(' + CASE
WHEN Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5)) = -1 THEN 'Max'
ELSE Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5))
END + ')' AS [Type]
FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'EMP_TABLE'

Output coming as:
NAME Null? Type
id    No NULL
CODE    YES varchar(20)
date    YES Null

Requiredoutput:

NAME Null? Type
id    No numeric(38)
CODE    YES varchar(20)
date    YES date


Any one can please help me on this..

Regards
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3048 Visits: 3625
Because for INT and DECIMAL the value for CHARACTER_MAXIMUM_LENGTH is a NULL value. Adding a NULL value to a string will result in a NULL value.
Change the CASE statement to:
, DATA_TYPE
      + CASE
         WHEN CHARACTER_MAXIMUM_LENGTH IS NULL
            THEN ''
         WHEN Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5)) = - 1
            THEN '(Max)'
         ELSE '(' + Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5)) + ')'
      END AS [Type]



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
toravi.pentaho
toravi.pentaho
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 5
Hi,

I tried with this logic also,But this time it is showing datatype as numeric but it is not showing value(range.like numeric(38))Here we can refer numeric_precision to get that '38' as range value

DATA_TYPE
      + CASE
         WHEN CHARACTER_MAXIMUM_LENGTH IS NULL
            THEN ''
         WHEN Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5)) = - 1
            THEN '(Max)'
         ELSE '(' + Cast(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(5)) + ')'
      END AS [Type]
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3048 Visits: 3625
You can place this code as the first CASE statement:
WHEN DATA_TYPE = 'decimal' 
   THEN '(' + Cast(NUMERIC_PRECISION AS VARCHAR(5)) + ',' + Cast(NUMERIC_SCALE AS VARCHAR(5)) + ')'




You can expand the CASE statement the same way for all other different datatypes...

** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search