I have a function a STRING_AGG function which is throwing error
Error message: STRING_AGG aggregation result exceeded the limit of
8000 bytes. Use LOB types to avoid result truncation.
As this Documentation, says I need to convert to varchar(max)
Current:
STRING_AGG(CONCAT([CT].[DeviceType] , ': ' , [COM].[Address]) , ', ')) AS [Contact]
Try:
STRING_AGG(CONVERT(VARCHAR(MAX),(CONCAT([CT].[DeviceType] , ': ' , [COM].[Address])) , ', ')) AS [Contact]
But it is throwing error:
The STRING_AGG function requires 2 argument(s).
What am I doing wrong? Regards
You have put the separator within the concat, it should be the second parameter:
STRING_AGG(CONVERT(VARCHAR(MAX),(CONCAT([CT].[DeviceType] , ': ' , [COM].[Address])) ), ', ') AS [Contact]
Related
Do we have a workaround for the T-SQL CONCAT_WS function? This one throws an error that
is not a recongized built in function name
I presume this is not compatible with my version.
I am currently trying to get a proper format for my full name where MiddleName is or isn't NULL.
First, SQL Server now supports CONCAT_WS(). So, using the built-in function is the simplest "work-around".
As an alternative:
select stuff( (coalesce(', ' + col1, '') +
coalesce(', ' + col2, '') +
coalesce(', ' + col3, '')
), 1, 2, '') as concat_ws
The separator and "2" need to be consistent, of course.
I'm trying to do a comparison of data between 2 tables, where I need to join multiple columns as a composite key to get a unique identifier. I'm using a CTE and the code I have is:
WITH SuburbDataTest AS (
SELECT *
, CAST(Address AS NVARCHAR(100))+' ' +CAST(LivingAddress AS NVARCHAR(2))
+ ' ' + CAST(StartDate AS NVARCHAR(11))+ ' ' +CAST(AddressTypeId AS NVARCHAR(1))
+ ' ' +CAST(SuburbId AS NVARCHAR(1))AS SuburbDataTestColumn
FROM [mig].[ConsumerAddressMigration]
WHERE SuburbId is NOT NULL
)
SELECT *
FROM SuburbDataTest staging
WHERE SuburbDataTestColumn IN (
SELECT Address+' ' +CAST(LivingAddress AS NVARCHAR(2))+ ' '+CAST(StartDate AS NVARCHAR(11))
+ ' ' +CAST(AddressTypeId AS NVARCHAR(1))+ ' ' +CAST(SuburbId AS NVARCHAR(1)) AS SuburbDataTestColumn
FROM [dbo].[tblConsumerAddress]
)
Unfortunately I'm getting
Arithmetic overflow error converting expression to data type nvarchar.
Any ideas?
This happens when you are converting a number to a string -- and the string is not big enough. I would guess this is the problem:
CAST(SuburbId AS NVARCHAR(1))
If SuburbId is a number larger than 9, then this will generate an error. Or, for that matter if the value is negative you'll get the same error as well.
i have code like this
SELECT ST_GeomFromText('POINT(replace( koordinat, ',', ' '))');
when i ran this query. showing error like this ? how to solve this probem..
ERROR: syntax error at or near "'))'"
LINE 1: ...ECT ST_GeomFromText('POINT(replace( koordinat, ',', ' '))');
First, you must build a string using the concatenate operator ||:
'POINT('|| replace( koordinat, ',', ' ') ||')'
Furthermore is recommended to set the SRID of the output geometry by passing a SRID as second parameter to the function ST_GeomFromText(). Assuming you have lon/lat coordinates, you will use 4326 as SRID:
SELECT ST_GeomFromText('POINT('||replace( koordinat, ',', ' ') || ')', 4326)
FROM your_table;
I want to use TRANSLATE to replace invalid XML characters in a string that's used in XMLAGG instead of nested REPLACE due to the large number of characters that need to be replaced:
select c.key,
cast( substr( xmlserialize( xmlagg( xmltext( concat( ' | ',
replace(replace(replace(replace(c.comment_text,chr(160),' '), chr(191),' '), chr(176),' '), chr(190),' ')
--translate(c.comment_text, ' ', chr(160)||chr(176)||chr(190)||chr(191))
) ) order by c.comment_date desc ) as clob ), 3, 4000 ) as varchar2) as sum_comment
from comments c
where c.comment_date > current_timestamp - 90
group by c.key
If I run this outside of the XMLAGG I can see that both the TRANSLATE and REPLACE are removing the invalid characters, but inside the XMLAGG I get the error I'm trying to avoid when using TRANSLATE:
An illegal XML character "#xA0" was found in an SQL/XML expression or function argument that begins with string " | A".
I have a stored procedure that is getting information from my employee table and returning the data.
There are 3 columns that are used:
B.[SiloDesc] + ' (' + B.[TitleDesc] + ') ' + B.[SkillSetDesc] as SkillSetDesc,
My issue is, if one of those happens to be null, it wont display any of the data. What is the best way to have it include the data regardless of if one of those fields are null.
You could use coalesce() or isnull() for each individual column... or you could simply use...
CONCAT ( string_value1, string_value2 [, string_valueN ] )
Takes a variable number of string arguments and concatenates them into a single string. It requires a minimum of two input values; otherwise, an error is raised. All arguments are implicitly converted to string types and then concatenated. Null values are implicitly converted to an empty string.
A.: Remove the parentheses when TitleDesc is null:
select concat(B.[SiloDesc], ' (' + B.[TitleDesc] + ')', ' ' + B.[SkillSetDesc])
Because of the way null is treated in sql, the expression ' (' + null + ')' results in null which concat() will treat as an empty string... which is kind of nice as it effectively removes the parentheses if the value is null.
B.: Keep the parentheses regardless:
select concat(B.[SiloDesc], ' (', B.[TitleDesc], ') ', B.[SkillSetDesc])
Samples:
select concat('john', ' (' + null + ')', ' adams') -- john adams
select concat('john', ' (test)', ' ' + null) -- john (test)
select concat('john', ' (the man)', ' adams') -- john (the man) adams
isnull(B.[SiloDesc], '')
+ ' (' + isnull(B.[TitleDesc], '') + ') '
+ isnull(B.[SkillSetDesc], '') as SkillSetDesc,