SQL uniqueidentifier

Encountered a SQL problem in the project:

select n.RootId as NodeId,r.PLACE_NAME as Node,Count(n.RootId) as Count,c.Year,c.Month,c.Day,c.Hour from 
connection.Connection c
inner join location.Node n on n.NETBAR_WACODE = c.NETBAR_WACODE
and n.RootId is not null
inner join location.Node r on r.NETBAR_WACODE = n.RootId
where c.CreatedDate >= @StartDate and c.CreatedDate <= @EndDate
group by n.RootId,r.PLACE_NAME,c.Year,c.Month,c.Day,c.Hour
order by c.Year desc,c.Month desc,c.Day desc,c.Hour desc

When the above SQL is executed, the following error message will be reported:

Insufficient result space to convert uniqueidentifier value to char.

 

 

Then I checked the concept of Uniqueidentifier

It is found that the uniqueidentifier storage size is 16 bytes, 16 bytes = 32 characters,
but generally our GUID: 52337445-56FD-456E-9AF4-F83CFC5C4016 The length is 36, how can it be stored?

the answer is:

GUID does not store the four connection numbers when it is stored, so it is 36-4=32. The characters of GUID are all hexadecimal, a hexadecimal is represented by four bits, a byte is 8 bits, So 32 * 4 /8 = 16 bytes, which is at the data storage level.
The GUID found is a specific "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" format composed of data plus connectors, so the number of characters is 36, which is the representation level.

So back to the top SQL is actually just one less conversion, plus

Convert(varchar(36),n.RootId) as NodeId

That's it, the main thing to note is that the varchar defined by the first parameter of the Convert function here must be >=36, you can't just use varchar~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324660194&siteId=291194637
SQL