Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Pass null value to the Charater Max Length to the column of a tablefor datatypes like int,datetime etc?

With below Query I am getting the zero as Character Max Length to data types like int,datetime etc. Also how to pass if its decimal data type. Varchar works fine.

SQL
SELECT  COLUMN_NAME + " " + DATA_TYPE + "(" + CONVERT(varchar(10),isnull(CHARACTER_MAXIMUM_LENGTH,""))+")" + "How ;,"
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = "PageAccessLog" ORDER BY ORDINAL_POSITION
Posted
Updated 18-Mar-15 19:49pm
v4
Comments
George Jonsson 19-Mar-15 1:50am    
I tried to fix the formatting of your question, but I am not sure I got it right.

1 solution

Following query will put '' in braces:
SQL
SELECT  COLUMN_NAME + ' ' + DATA_TYPE + '(' + isnull(CONVERT(varchar(10),CHARACTER_MAXIMUM_LENGTH),'')+')' + 'How ;,'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'PageAccessLog'
ORDER BY ORDINAL_POSITION


However if you want to avoid braces as well and handling special case for decimal:

SQL
SELECT  COLUMN_NAME + ' ' + DATA_TYPE +
case when CHARACTER_MAXIMUM_LENGTH is null then
    case when DATA_TYPE = 'Decimal' then '(' + CONVERT(varchar(10),Numeric_Precision) + ', ' + CONVERT(varchar(10),NUMERIC_SCALE) +')'
    else '' end
else '(' + isnull(CONVERT(varchar(10),CHARACTER_MAXIMUM_LENGTH),'')+')' end + ' How ;,'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'PageAccessLog'
ORDER BY ORDINAL_POSITION
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900