How to CONCAT two table columns into one column in SQL Server - sql-server

I am new to SQL Server. I have a script. Please check the script first
SELECT
WOtask.PK,
WOPK,
TaskNo,
TaskAction = CASE
WHEN WOTask.AssetPK IS NOT NULL
THEN '<b>' + Asset.AssetName + ' [' + Asset.AssetID + ']</b> ' +
CASE
WHEN Asset.Vicinity IS NOT NULL
AND Asset.Vicinity <> ''''
THEN RTRIM(Asset.Vicinity) + ': '
ELSE ''''
END + WOtask.TaskAction + CASE
WHEN CONVERT(varchar, ValueLow) IS NOT NULL AND
CONVERT(varchar, ValueHi) IS NOT NULL AND
Spec = 1 THEN ' ('+ 'Range:'+ '' + CONVERT(VARCHAR,CONVERT(FLOAT,ISNULL(ValueLow,0))) + ' - ' + CONVERT(VARCHAR,CONVERT(FLOAT,ISNULL(ValueHi,0))) + ')'
ELSE ''
END
ELSE WOtask.TaskAction + CASE
WHEN CONVERT(varchar, ValueLow) IS NOT NULL AND
CONVERT(varchar, ValueHi) IS NOT NULL AND
Spec = 1 THEN ' ('+ 'Range:'+ '' + CONVERT(VARCHAR,CONVERT(FLOAT,ISNULL(ValueLow,0))) + ' - ' + CONVERT(VARCHAR,CONVERT(FLOAT,ISNULL(ValueHi,0))) + ')'
ELSE ''
END
END ,
Rate,
Measurement,
Initials,
Fail,
Complete,
Header,
LineStyle,
WOtask.Comments,
WOtask.NotApplicable,
WOTask.Photo1,
WOTask.Photo2
FROM
WOtask WITH (NOLOCK)
LEFT OUTER JOIN
Asset WITH (NOLOCK) ON Asset.AssetPK = WOTask.AssetPK
LEFT OUTER JOIN
AssetSpecification ON AssetSpecification.PK = WOTask.AssetSpecificationPK
WHERE
(WOPK IN (SELECT WOPK
FROM WO WITH (NOLOCK)
LEFT OUTER JOIN Asset WITH (NOLOCK) ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK) ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10939)
)
ORDER BY
WOPK, TaskNo
and here is the output :
now my requirement is to concat TaskAction and Comment column into single column . i would like to give one example of my requirement : the column must show TaskAction value plus Comment: hi(comment column value) . i have been trying to put CONVERT Function for concat two columns but i couldn't get the result as i have been applying convert function to wrong place .
Please help me with some solution. Thanks in advance

Here is the SQL you should use:
SELECT WOtask.PK
, WOPK
, TaskNo
, TaskAction = CASE
WHEN WOtask.AssetPK IS NOT NULL THEN
'<b>' + Asset.AssetName + ' [' + Asset.AssetID + ']</b> ' + CASE
WHEN Asset.Vicinity IS NOT NULL
AND Asset.Vicinity <> '''' THEN
RTRIM(Asset.Vicinity) + ': '
ELSE
''''
END + WOtask.TaskAction
+ CASE
WHEN CONVERT(VARCHAR, ValueLow) IS NOT NULL
AND CONVERT(VARCHAR, ValueHi) IS NOT NULL
AND Spec = 1 THEN
' (' + 'Range:' + '' + CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueLow, 0))) + ' - '
+ CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueHi, 0))) + ')'
ELSE
''
END
ELSE
WOtask.TaskAction
+ CASE
WHEN CONVERT(VARCHAR, ValueLow) IS NOT NULL
AND CONVERT(VARCHAR, ValueHi) IS NOT NULL
AND Spec = 1 THEN
' (' + 'Range:' + '' + CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueLow, 0))) + ' - '
+ CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueHi, 0))) + ')'
ELSE
''
END
END
, Rate
, Measurement
, Initials
, Fail
, Complete
, Header
, LineStyle
, WOtask.Comments
, WOtask.NotApplicable
, WOtask.Photo1
, WOtask.Photo2
, ISNULL(
CASE
WHEN WOtask.AssetPK IS NOT NULL THEN
'<b>' + Asset.AssetName + ' [' + Asset.AssetID + ']</b> ' + CASE
WHEN Asset.Vicinity IS NOT NULL
AND Asset.Vicinity <> '''' THEN
RTRIM(Asset.Vicinity) + ': '
ELSE
''''
END + WOtask.TaskAction
+ CASE
WHEN CONVERT(VARCHAR, ValueLow) IS NOT NULL
AND CONVERT(VARCHAR, ValueHi) IS NOT NULL
AND Spec = 1 THEN
' (' + 'Range:' + '' + CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueLow, 0))) + ' - ' + CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueHi, 0)))
+ ')'
ELSE
''
END
ELSE
WOtask.TaskAction
+ CASE
WHEN CONVERT(VARCHAR, ValueLow) IS NOT NULL
AND CONVERT(VARCHAR, ValueHi) IS NOT NULL
AND Spec = 1 THEN
' (' + 'Range:' + '' + CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueLow, 0))) + ' - ' + CONVERT(VARCHAR, CONVERT(FLOAT, ISNULL(ValueHi, 0)))
+ ')'
ELSE
''
END
END
, ''
) + ISNULL(WOtask.Comments, '') AS [Single Column]
FROM WOtask WITH (NOLOCK)
LEFT OUTER JOIN Asset WITH (NOLOCK) ON Asset.AssetPK = WOtask.AssetPK
LEFT OUTER JOIN AssetSpecification ON AssetSpecification.PK = WOtask.AssetSpecificationPK
WHERE (WOPK IN
(
SELECT WOPK
FROM WO WITH (NOLOCK)
LEFT OUTER JOIN Asset WITH (NOLOCK) ON Asset.AssetPK = WO.AssetPK
LEFT OUTER JOIN AssetHierarchy WITH (NOLOCK) ON AssetHierarchy.AssetPK = WO.AssetPK
WHERE WO.WOPK = 10939
)
)
ORDER BY WOPK
, TaskNo;

Related

Making monetary values that are 0 or 0.00 NULL from formatted TSQL statement

I have the following SQL statement
SELECT DISTINCT
ItemID,
Taxlines = STUFF((SELECT CHAR(182) + RTRIM(IsNull(TT.Code, '')) + '$' + RTRIM(IsNull(TaxLink.BaseAmount, '')) +
'$' + RTRIM(IsNull(TaxLink.BaseQuantity, '')) + '$' + RTRIM(IsNull(TR.Code, '')) + '$' + RTRIM(IsNull(TOC.Code, '')) +
'$' + RTRIM(IsNull(TaxLink.Amount, '')) + '$' + RTRIM(IsNull(TPM.Code, ''))
FROM
[Declaration].[Taxline] [TaxLink]
LEFT OUTER JOIN
Declaration.TaxType TT ON TT.TaxTypeID = TaxLink.TypeID
LEFT OUTER JOIN
Declaration.TaxRate TR ON TR.TaxRateID = TaxLink.RateID
LEFT OUTER JOIN
Declaration.TaxOverrideCode TOC ON TOC.TaxOverrideCodeID = TaxLink.OverrideCodeID
LEFT OUTER JOIN
Declaration.TaxPaymentMethod TPM ON TPM.TaxPaymentMethodID = TaxLink.PaymentMethodID
WHERE
Tax.ItemID = ItemID
FOR XML PATH('')), 1, 1, '')
FROM
[Declaration].[Taxline] Tax
WHERE
Tax.ItemID IS NOT NULL
Which produces an output like:
A00$0.00$0$F$$0.00$D¶B00$0.00$0$S$$0.00$D
I need to replace any monetary value 0 or 0.00 with either NULL or '' don't mind which.
This doesn't include A00 or B00 I do however need to keep the $ separators
So the above example needs to look like this:
A00$$$F$$$D¶B00$$0$S$$$D
Select Distinct ItemID,
Taxlines = STUFF(( Select CHAR(182) + CASE WHEN TT.Code IS NULL THEN '' ELSE TT.Code END
+ '$' + RTRIM(CASE WHEN TaxLink.BaseAmount IS NULL THEN '' ELSE CAST(TaxLink.BaseAmount AS nvarchar(15)) END)
+ '$' + RTRIM(CASE WHEN TaxLink.BaseQuantity IS NULL THEN '' ELSE CAST(CAST(TaxLink.BaseQuantity AS decimal(15,3)) AS nvarchar(15)) END)
+ '$' + RTRIM(CASE WHEN TR.Code IS NULL THEN '' ELSE TR.Code END)
+ '$' + RTRIM(CASE WHEN TOC.Code IS NULL THEN '' ELSE TOC.Code END
+ '$' + RTRIM(CASE WHEN TaxLink.Amount IS NULL THEN '' ELSE CAST(TaxLink.Amount AS nvarchar(15)) END)
+ '$' + RTRIM(CASE WHEN TPM.Code IS NULL THEN '' ELSE TPM.Code END))
FROM [Declaration].[Taxline] [TaxLink]
LEFT OUTER JOIN Declaration.TaxType TT on TT.TaxTypeID = TaxLink.TypeID
LEFT OUTER JOIN Declaration.TaxRate TR on TR.TaxRateID = TaxLink.RateID
LEFT OUTER JOIN Declaration.TaxOverrideCode TOC on TOC.TaxOverrideCodeID = TaxLink.OverrideCodeID
LEFT OUTER JOIN Declaration.TaxPaymentMethod TPM on TPM.TaxPaymentMethodID = TaxLink.PaymentMethodID
WHERE Tax.ItemID = ItemID
FOR XML PATH('')
), 1, 1, '')
FROM [Declaration].[Taxline] Tax

Following query when executed on sql server , for some server it showing all columns but for some server it showing only last column?

Following query when executed on sql server , For some server for table shipments it showing all columns but for some server it showing only last column? Any setting need to change where it showing only last column? Thanks in advance.
DECLARE #Cols VARCHAR(MAX)
SET #Cols = ''
SELECT #Cols = #Cols + ' #' + REPLACE(c.NAME, ' ', '') + ' ' + t.NAME + CASE
WHEN t.NAME IN (
'nvarchar'
,'varchar'
,'char'
,'nchar'
)
THEN '(' + CASE c.max_length
WHEN - 1
THEN 'MAX'
ELSE CAST(c.max_length AS VARCHAR(10))
END + ')'
WHEN t.NAME IN ('decimal')
THEN '(' + CAST(c.precision AS VARCHAR(10)) + ',' + CAST(c.scale AS VARCHAR(10)) + ')'
ELSE ''
END + ',' + CHAR(10)
--c.name + ' ' + t.name --+ '(' + CAST(c.max_length as varchar(10)) + ',' + CAST(c.precision as varchar(10)) + ')' + ','
--+ CHAR(10)
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
JOIN sys.objects o ON c.object_id = o.object_id
WHERE OBJECT_NAME(c.object_id) = 'shipments'
AND o.schema_id = SCHEMA_ID('dbo')
AND t.NAME NOT IN (
'text'
,'NTEXT'
,'image'
)
ORDER BY c.column_id
SELECT #Cols

Need a programatic way to get the names, arguments and return types for SQL Server built-in functions

For postgres it is possible to obtain metadata for built-in functions via SQL queries like
SELECT
proname as name,
pg_catalog.pg_get_function_arguments(pg_proc.oid) as input,
format_type(pg_proc.prorettype, null) as returntype,
prosrc as definition
FROM pg_proc;
However for SQL Server, I cannot find any such set of tables or functions. Is it possible?
This should give you what you're looking for... Plus maybe a little extra. ;)
SELECT
o.name,
o.object_id,
o.type_desc,
px.parameters,
returns = ISNULL(rx.returns, 'table'),
tc.table_cols
FROM
sys.objects o WITH (NOLOCK)
CROSS APPLY (
SELECT parameters = STUFF((
SELECT
CONCAT(', ', p.name, ' ', TYPE_NAME(p.user_type_id), ' ', psl.precision_scale_len)
FROM
sys.parameters p WITH (NOLOCK)
CROSS APPLY ( VALUES (CASE
WHEN p.user_type_id IN (34,35,36,40,48,52,56,58,59,60,61,62,98,99,104,122,127,128,129,130,189,241,256) THEN ''
WHEN p.user_type_id IN (106,108) THEN '(' + CONVERT(VARCHAR(10), p.precision) + ',' + CONVERT(VARCHAR(10), p.scale) + ')'
WHEN p.user_type_id IN (41,42,43) THEN '(' + CONVERT(VARCHAR(10), p.scale) + ')'
WHEN p.user_type_id IN (165,167,173,175) THEN '(' + CASE WHEN p.max_length = -1 THEN 'max' ELSE CONVERT(VARCHAR(10), p.max_length) END + ')'
WHEN p.user_type_id IN (231,239) THEN '(' + CASE WHEN p.max_length = -1 THEN 'max' ELSE CONVERT(VARCHAR(10), p.max_length / 2) END + ')'
END) ) psl (precision_scale_len)
WHERE
o.object_id = p.object_id
AND p.is_output = 0
FOR XML PATH ('')
), 1, 2, '')
) px (parameters)
OUTER APPLY (
SELECT
CONCAT(r.name, ' ', TYPE_NAME(r.user_type_id), ' ', psl.precision_scale_len)
FROM
sys.parameters r WITH (NOLOCK)
CROSS APPLY ( VALUES (CASE
WHEN r.user_type_id IN (34,35,36,40,48,52,56,58,59,60,61,62,98,99,104,122,127,128,129,130,189,241,256) THEN ''
WHEN r.user_type_id IN (106,108) THEN '(' + CONVERT(VARCHAR(10), r.precision) + ',' + CONVERT(VARCHAR(10), r.scale) + ')'
WHEN r.user_type_id IN (41,42,43) THEN '(' + CONVERT(VARCHAR(10), r.scale) + ')'
WHEN r.user_type_id IN (165,167,173,175) THEN '(' + CASE WHEN r.max_length = -1 THEN 'max' ELSE CONVERT(VARCHAR(10), r.max_length) END + ')'
WHEN r.user_type_id IN (231,239) THEN '(' + CASE WHEN r.max_length = -1 THEN 'max' ELSE CONVERT(VARCHAR(10), r.max_length / 2) END + ')'
END) ) psl (precision_scale_len)
WHERE
o.object_id = r.object_id
AND r.is_output = 1
) rx ([returns])
CROSS APPLY (
SELECT STUFF((
SELECT
CONCAT(', ', c.name, ' ', TYPE_NAME(c.user_type_id), ' ', psl.precision_scale_len)
FROM
sys.columns c WITH (NOLOCK)
CROSS APPLY ( VALUES (CASE
WHEN c.user_type_id IN (34,35,36,40,48,52,56,58,59,60,61,62,98,99,104,122,127,128,129,130,189,241,256) THEN ''
WHEN c.user_type_id IN (106,108) THEN '(' + CONVERT(VARCHAR(10), c.precision) + ',' + CONVERT(VARCHAR(10), c.scale) + ')'
WHEN c.user_type_id IN (41,42,43) THEN '(' + CONVERT(VARCHAR(10), c.scale) + ')'
WHEN c.user_type_id IN (165,167,173,175) THEN '(' + CASE WHEN c.max_length = -1 THEN 'max' ELSE CONVERT(VARCHAR(10), c.max_length) END + ')'
WHEN c.user_type_id IN (231,239) THEN '(' + CASE WHEN c.max_length = -1 THEN 'max' ELSE CONVERT(VARCHAR(10), c.max_length / 2) END + ')'
END) ) psl (precision_scale_len)
WHERE
o.object_id = c.object_id
FOR XML PATH('')), 1, 2, '')
) tc (table_cols)
WHERE
o.type IN ('AF','FN','FS','FT','IF');

How to add DISTINCT to a column in select query

Can you help me how to use DISTINCT in this query?
DECLARE #ReadData NVARCHAR(MAX);
SET #ReadData = 'SELECT ROW_NUMBER() OVER (ORDER BY RM.ReceiverDate) AS [S. No.]
,DISTINCT RM.ReceiverId
,RM.ReceiverDateas [Receiver Date]
,VM.VendorName as [Vendor Name]
,VM.VendorID
FROM Receiver RM
LEFT OUTER JOIN VendorMaster VM on VM.VendorID = RM.VendorId
WHERE ['+ #ColumnName + '] LIKE
CASE WHEN ' + #Filter+ ' = 0 THEN ''%' + #Value + '%'''+
' WHEN ' + #Filter + ' = 1 THEN '''+ #Value + '%'''+
' WHEN ' + #Filter + ' = 2 THEN ''%' + #Value +''' END
AND VM.IsActive = 1
AND RM.VendorID = CASE WHEN ' + CONVERT(VARCHAR, #VendorID) + ' = ''-1'' THEN RM.VendorID
ELSE '+ CONVERT(VARCHAR, #VendorID) +' END
AND (RM.ReceiverDate BETWEEN ' + CHAR(39) + CONVERT(VARCHAR(23), #FromDate, 126) + CHAR(39) +' AND ' + CHAR(39) + CONVERT(VARCHAR(23), #ToDate, 126) + CHAR(39) +')
ORDER BY RM.ReceiverDate '
My question is: how to add DISTINCT to the ReceiverId ?
I get this error:
Incorrect syntax near the keyword 'DISTINCT'.
in the C# code behind
with cte
as
(
Select ROW_NUMBER() OVER (PARTITION BY ReceiverId ORDER BY ReceiverId ) AS dupCount, *
from(
SELECT ROW_NUMBER() OVER (ORDER BY ReceivedDate) AS [S. No.]
,RM.ReceiverId
,RM.ReceiverName'
,VM.VendorName
, XYZ columns .....
From Receiver RM
LEFT OUTER JOIN VendorMaster VM on VM.VendorID = RM.VendorId
) tab
)
select S. No.
,ReceiverId
,ReceiverName
,VendorName
, XYZ columns
from cte
where dupcount=1;
try using cte.
DECLARE #ReadData NVARCHAR(MAX);
set #ReadData = 'SELECT *
,ROW_NUMBER() OVER (ORDER BY ReceivedDate) AS [S. No.]
FROM (SELECT DISTINCT ReceiverId,
ReceivedDate
,ReceiverName
, XYZ columns .....
From Receiver .........
INNER JOIN .......)'
You can't use the row_number and distinct on same level.

How to get the scripts of all the user defined data types

How to get the scripts of all the user defined data types through a single query.
Try this:
SET NOCOUNT ON
SELECT 'USE ' + QUOTENAME(DB_NAME(), '[') + '
GO';
SELECT '
IF EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N''' + st.[name] + ''' AND ss.name = N''' + ss.[name] + ''')
DROP TYPE ' + QUOTENAME(ss.name, '[') + '.' + QUOTENAME(st.name, '[') + '
GO
CREATE TYPE ' + QUOTENAME(ss.name, '[') + '.' + QUOTENAME(st.name, '[') + ' FROM ' +
QUOTENAME(bs.[name], '[') +
CASE bs.[name]
WHEN 'char' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length) + ')' END)
WHEN 'nchar' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length/2) + ')' END)
WHEN 'varchar' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length) + ')' END)
WHEN 'nvarchar' THEN (CASE ISNULL(st.max_length, 0) WHEN 0 THEN '' WHEN -1 THEN '(MAX)' ELSE '(' + convert(varchar(10), st.max_length/2) + ')' END)
WHEN 'numeric' THEN (CASE ISNULL(st.[precision], 0) WHEN 0 THEN '' ELSE '(' + convert(varchar(10), st.[precision]) + ', ' + convert(varchar(10), st.[scale]) + ')' END)
WHEN 'decimal' THEN (CASE ISNULL(st.[precision], 0) WHEN 0 THEN '' ELSE '(' + convert(varchar(10), st.[precision]) + ', ' + convert(varchar(10), st.[scale]) + ')' END)
WHEN 'varbinary' THEN (CASE st.max_length WHEN -1 THEN '(max)' ELSE '(' + convert(varchar(10), st.max_length) + ')' END)
ELSE ''
END +
'
GO
'
FROM sys.types st
INNER JOIN sys.schemas ss ON st.[schema_id] = ss.[schema_id]
INNER JOIN sys.types bs ON bs.[user_type_id] = st.[system_type_id]
WHERE st.[is_user_defined] = 1 -- exclude system types
ORDER BY st.[name], ss.[name]
Disclaimer: I am not the author of this script. Just that I have it in my personal library. Unfortunately authors name is not there in it.
Here's a script that is actually working.
Creates a script for all user-defined scalar & table-types (not tested with CLR-types):
-- http://www.sqlines.com/sql-server-to-oracle/create_type
SELECT
sch.name AS UDT_SCHEMA_NAME
,userDefinedTypes.name AS UDT_TYPE_NAME
,
N'IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N''' + REPLACE(userDefinedTypes.name, '''', '''''') + N''' AND ss.name = N''' + REPLACE(sch.name, '''', '''''') + N''') '
+ NCHAR(13) + NCHAR(10)
+
CASE WHEN userDefinedTypeProperties.IsTableType = 1
THEN N'CREATE TYPE ' + QUOTENAME(sch.name) + '.' + QUOTENAME(userDefinedTypes.name) + ' AS TABLE (
' + tAllColumns.column_definition + N'
); '
ELSE
+ N'CREATE TYPE ' + QUOTENAME(sch.name) + '.' + QUOTENAME(userDefinedTypes.name)
+ N' FROM '
+ tBaseTypeComputation.baseTypeName
+ CASE WHEN userDefinedTypeProperties.is_nullable = 0 THEN N' NOT NULL' ELSE N'' END
+ N'; '
END AS SqlCreateUdt
FROM sys.types AS userDefinedTypes
INNER JOIN sys.schemas AS sch
ON sch.schema_id = userDefinedTypes.schema_id
LEFT JOIN sys.table_types AS userDefinedTableTypes
ON userDefinedTableTypes.user_type_id = userDefinedTypes.user_type_id
LEFT JOIN sys.types AS systemType
ON systemType.system_type_id = userDefinedTypes.system_type_id
AND systemType.is_user_defined = 0
OUTER APPLY
(
SELECT
userDefinedTypes.is_nullable
,userDefinedTypes.precision AS NUMERIC_PRECISION
,userDefinedTypes.scale AS NUMERIC_SCALE
,userDefinedTypes.max_length AS CHARACTER_MAXIMUM_LENGTH
,CASE WHEN userDefinedTableTypes.user_type_id IS NULL THEN 0 ELSE 1 END AS IsTableType
,CONVERT(smallint,
CASE -- datetime/smalldatetime
WHEN userDefinedTypes.system_type_id IN (40, 41, 42, 43, 58, 61) THEN ODBCSCALE(userDefinedTypes.system_type_id, userDefinedTypes.scale)
END
) AS DATETIME_PRECISION
) AS userDefinedTypeProperties
OUTER APPLY
(
SELECT
systemType.name
+
CASE
WHEN systemType.name IN ('char', 'varchar', 'nchar', 'nvarchar', 'binary', 'varbinary')
THEN N'('
+
CASE WHEN userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX'
ELSE CONVERT
(
varchar(4)
,userDefinedTypeProperties.CHARACTER_MAXIMUM_LENGTH
)
END
+ N')'
WHEN systemType.name IN ('decimal', 'numeric')
THEN N'(' + CONVERT(varchar(4), userDefinedTypeProperties.NUMERIC_PRECISION) + N', ' + CONVERT(varchar(4), userDefinedTypeProperties.NUMERIC_SCALE) + N')'
WHEN systemType.name IN ('time', 'datetime2', 'datetimeoffset')
THEN N'(' + CAST(userDefinedTypeProperties.DATETIME_PRECISION AS national character varying(36)) + N')'
ELSE N''
END AS baseTypeName
) AS tBaseTypeComputation
OUTER APPLY
(
SELECT
(
SELECT
-- ,clmns.is_nullable
-- ,tComputedProperties.ORDINAL_POSITION
-- ,tComputedProperties.COLUMN_DEFAULT
CASE WHEN tComputedProperties.ORDINAL_POSITION = 1 THEN N' ' ELSE N',' END
+ QUOTENAME(clmns.name)
+ N' '
+ tComputedProperties.DATA_TYPE
+
CASE
WHEN tComputedProperties.DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'binary', 'varbinary')
THEN N'('
+
CASE WHEN tComputedProperties.CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX'
ELSE CONVERT
(
varchar(4)
,tComputedProperties.CHARACTER_MAXIMUM_LENGTH
)
END
+ N')'
WHEN tComputedProperties.DATA_TYPE IN ('decimal', 'numeric')
THEN N'(' + CONVERT(varchar(4), tComputedProperties.NUMERIC_PRECISION) + N', ' + CONVERT(varchar(4), tComputedProperties.NUMERIC_SCALE) + N')'
WHEN tComputedProperties.DATA_TYPE IN ('time', 'datetime2', 'datetimeoffset')
THEN N'(' + CAST(tComputedProperties.DATETIME_PRECISION AS national character varying(36)) + N')'
ELSE N''
END
+ CASE WHEN tComputedProperties.is_nullable = 0 THEN N' NOT NULL' ELSE N'' END
+ NCHAR(13) + NCHAR(10)
AS [text()]
FROM sys.columns AS clmns
INNER JOIN sys.types AS t ON t.system_type_id = clmns.system_type_id
LEFT JOIN sys.types ut ON ut.user_type_id = clmns.user_type_id
OUTER APPLY
(
SELECT
33 As bb
,COLUMNPROPERTY(clmns.object_id, clmns.name, 'ordinal') AS ORDINAL_POSITION
,COLUMNPROPERTY(clmns.object_id, clmns.name, 'charmaxlen') AS CHARACTER_MAXIMUM_LENGTH
,COLUMNPROPERTY(clmns.object_id, clmns.name, 'octetmaxlen') AS CHARACTER_OCTET_LENGTH
,CONVERT(nvarchar(4000), OBJECT_DEFINITION(clmns.default_object_id)) AS COLUMN_DEFAULT
,clmns.is_nullable
,t.name AS DATA_TYPE
,CONVERT(tinyint,
CASE -- int/decimal/numeric/real/float/money
WHEN clmns.system_type_id IN (48, 52, 56, 59, 60, 62, 106, 108, 122, 127) THEN clmns.precision
END
) AS NUMERIC_PRECISION
,CONVERT(int,
CASE -- datetime/smalldatetime
WHEN clmns.system_type_id IN (40, 41, 42, 43, 58, 61) THEN NULL
ELSE ODBCSCALE(clmns.system_type_id, clmns.scale)
END
) AS NUMERIC_SCALE
,CONVERT(smallint,
CASE -- datetime/smalldatetime
WHEN clmns.system_type_id IN (40, 41, 42, 43, 58, 61) THEN ODBCSCALE(clmns.system_type_id, clmns.scale)
END
) AS DATETIME_PRECISION
) AS tComputedProperties
WHERE clmns.object_id = userDefinedTableTypes.type_table_object_id
ORDER BY tComputedProperties.ORDINAL_POSITION
FOR XML PATH(''), TYPE
).value('.', 'nvarchar(MAX)') AS column_definition
) AS tAllColumns
WHERE userDefinedTypes.is_user_defined = 1
In addition to what vmvadivel said (that works like a charm!!) This is a script to do it for you, just dump that code in there and name the sql file getTypesSelect.sql and this will work:
#echo off
cls
set /p SName=Server Name :
set /p UName=User Name :
set /p Pwd=Password :
set /p DbName=Database Name :
set /p choice=ARE YOU SURE TO EXECUTE SCRIPTS in %DbName% (y/n) ?
if '%choice%'=='y' goto begin
goto end
:begin
if exist addUserDefinedTypes.sql del addUserDefinedTypes.sql
#echo on
SQLCMD -S %SName% -d %DbName% -U %UName% -i "getTypesSelect.sql" -o addUserDefinedTypes.txt -P %Pwd%
rename addUserDefinedTypes.txt addUserDefinedTypes.sql
#notepad addUserDefinedTypes.sql
:end
Remove the following line from the sql query above if you plan to run on a different database (which you probably will)
SELECT 'USE ' + QUOTENAME(DB_NAME(), '[') + '
GO';

Resources