SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Type lengths


Type lengths

Author
Message
anders-731262
anders-731262
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1067 Visits: 362
Comments posted to this topic are about the item Type lengths
Christian Buettner-167247
Christian Buettner-167247
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5235 Visits: 3889
Good Question!

The information in the MSDN link is hard to grasp in my opinion (it is a long way from reading "must be implicitly convertible" to understanding that the conversion may result in loss of data), but the example provided makes it very clear.

Thanks for this QOTD!

Best Regards,

Chris Büttner
SuperDBA-207096
SuperDBA-207096
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2963 Visits: 711
Nice one!

Coalesce is a pretty useful function - it comes in handy when dealing with nulls....
dunnjoe
dunnjoe
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1966 Visits: 208
Good question! I had to do some investigation of coalesce which looks like a very useful function.
douglascfast
douglascfast
SSC-Enthusiastic
SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)SSC-Enthusiastic (142 reputation)

Group: General Forum Members
Points: 142 Visits: 225
Did I miss something? If two columns are 1 in lengh, and the others are 9 "some text" and 14 "Some more text", then would not the count(*) give you 2 (1 length) 1(9 lenght) and 1(14 lenght).

I did this on my database and i got

Query1
-----------
2
1
1

Query2
-----------
4

Query3
-----------
2
1
1

Confused - - - as normal
Doug
rajankjohn
rajankjohn
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2005 Visits: 575
douglascfast (9/9/2008)
Did I miss something? If two columns are 1 in lengh, and the others are 9 "some text" and 14 "Some more text", then would not the count(*) give you 2 (1 length) 1(9 lenght) and 1(14 lenght).

I did this on my database and i got

Query1
-----------
2
1
1

Query2
-----------
4

Query3
-----------
2
1
1

Confused - - - as normal
Doug


I also got the same results. Anything missing?
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66609 Visits: 9671
Yes, that's 3 rows, 1 row, 3 rows...
Christian Buettner-167247
Christian Buettner-167247
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5235 Visits: 3889
Hi Doug,

thats exactly the purpose of the question.
If you execute
SELECT ISNULL(A,B) FROM #Test

you will get four 1-char strings because of the implicit conversion.
A
S
B
S


Since the length of these is 1, the group by returns only one row.

Hope this helps.

Best Regards,

Chris Büttner
StarNamer
StarNamer
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2762 Visits: 1992
I knew there was a reason I'd switched to preferring COALESCE over ISNULL... Smile

Derek
arehman626
arehman626
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 40
Let's go step by step.
CREATE TABLE #TEST(A varchar,B varchar(30)); ---> Field named A is of size 1 with type Varchar.

ISNULL(A,B) ---> means:evaluate the field A, (the first parameter), if has something other than a NULL then return it to me otherwise return me the value converted to Varchar size 1 (As A is Varchar (1)) from varchar size 30 in field B for current record.
So if you run

SELECT A, B, ISNULL(A,B),LEN(ISNULL(A,B)) FROM #Test

A B
---- ------------------------------ ---- -----------
A NULL A 1
NULL Some text S 1
B NULL B 1
NULL Some more text S 1

so if you now run
SELECT COUNT(*),LEN(ISNULL(A,B))
FROM #TEST
GROUP BY LEN(ISNULL(A,B));

you'll see
----------- -----------
4 1


And
SELECT A, B, ISNULL(B,A),LEN(ISNULL(B,A)) FROM #Test
A B
---- ------------------------------ ------------------------------ -----------
A NULL A 1
NULL Some text Some text 9
B NULL B 1
NULL Some more text Some more text 14
and run
SELECT COUNT(*),LEN(ISNULL(B,A))
FROM #TEST
GROUP BY LEN(ISNULL(B,A));
----------- -----------
2 1
1 9
1 14



Now let's talk about COALESCE(A,B)
COALESCE returns the first non-null expression among its arguments,
If you run
SELECT LEN(COALESCE(A,B)),COALESCE(A,B)
FROM #TEST
then
----------- ------------------------------
1 A
9 Some text
1 B
14 Some more text
so
SELECT COUNT(*),LEN(COALESCE(A,B))
FROM #TEST
GROUP BY LEN(COALESCE(A,B));
----------- -----------
2 1
1 9
1 14
.
I hope this has been useful.

Abdul Rehman
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search