Obtaining size of rows using DataLength and hard-coded column names fails

52 views Asked by At

I am trying to write a SQL query that will show me the records that are largest in size along with the User Names associated with the record.

SELECT 
    Data.Id,
    User.UserName, 
    (
    ISNULL(DATALENGTH(ColumnA), 1) + 
    ISNULL(DATALENGTH(ColumnB), 1) + 
    ISNULL(DATALENGTH(ColumnC), 1)
    ) AS SizeOfRow
FROM Data
    LEFT JOIN User ON Data.UserId = User.UserId 
ORDER BY SizeOfRow DESC

When I run this I get the error Conversion failed when converting from a character string to uniqueidentifier. I am not sure where I am going wrong here. If I remove the JOIN to the User table then the SQL runs without issue. However, I really want to include the UserName field and I am not sure why it's addition is causing a problem.

EDIT: I should mention that ColumnA is a datatype of UniqueIdentifier. While ColumnB and ColumnC are both varchar(max).

0

There are 0 answers