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


Tricky ...VARCHAR


Tricky ...VARCHAR

Author
Message
Bhuvnesh
Bhuvnesh
SSCrazy Eights
SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)SSCrazy Eights (8.8K reputation)

Group: General Forum Members
Points: 8796 Visits: 4077
CREATE
TABLE #TEST(A varchar(40),B varchar(30));
INSERT
INTO #TEST(A) VALUES('A');
INSERT
INTO #TEST(B) VALUES('Some text');
INSERT
INTO #TEST(A) VALUES('B');
INSERT
INTO #TEST(B) VALUES('Some more text');

------------------------------------------------------------
CREATE
TABLE #TEST2(A varchar,B varchar(30));
INSERT
INTO #TEST2(A) VALUES('A');
INSERT
INTO #TEST2(B) VALUES('Some text');
INSERT
INTO #TEST2(A) VALUES('B');
INSERT
INTO #TEST2(B) VALUES('Some more text');


Q1:
SELECT
COUNT(*)FROM #TEST GROUP BY LEN(ISNULL(B,A));
go
Q2:
SELECT
COUNT(*) from #TEST GROUP BY LEN(ISNULL(A,B));

Q3:
SELECT
COUNT(*)FROM #TEST2 GROUP BY LEN(ISNULL(B,A));
go
Q4:
SELECT
COUNT(*) from #TEST2 GROUP BY LEN(ISNULL(A,B));


if you see results of above query the functionality of LEN(ISNULL(B,A)) changes
just because ini #test2 i m declaring A as varchar instead of varchar(40)


Can any body explain WHY ???

i m apolgise that i m re-posting this QuestionSmileSmile

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)

Group: General Forum Members
Points: 148609 Visits: 45876
Already asked in the appropriate Question of the Day thread

http://www.sqlservercentral.com/Forums/Topic565885-1370-1.aspx

Please don't post multiple threads with the same question. It wastes people's time, it breaks replies up and results in people answering already answered questions.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


vinod.andani-874416
vinod.andani-874416
SSC Veteran
SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)SSC Veteran (277 reputation)

Group: General Forum Members
Points: 277 Visits: 82
Its because of ISNULL() function. ISNULL(exp1, expr2) always returns expr1 if expr1 is not null. Else expr2 typecasted to expr1. As column 'A' is of varchar with no length.. so default 1 character in length. When evaluating ISNULL(), when column A is null, it type casted value of columnB to 1 char i.e., trimmed rest of characters.. As column 'A' contained values "A" and "B".. so len(ISNULL(A,B)) returned 1 each. When ColumnA is NULL and columnB contained values 'Some text' and 'some more text'... it has taken first character because of expr1 in ISNULL(). So len(ISNULL(A,B)) again returned 1 each. Alltogether there are 4 1's. So when GROUP BY is applied, COUNT(*) gave you 4. :-)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)

Group: General Forum Members
Points: 148609 Visits: 45876
Please note: 3 year old thread.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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