Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDITED AND UNDELETED</strong></p> <p>Answer to A.: Yes, or you can use ISNULL() with the same result as COALESCE() in this case.</p> <p>Answer to B.: Do not convert a varchar to a varbinary to compare them, but <strong>understand the collation sort order when using aggregates.</strong></p> <p>I think this code snippet answers the count with NULL problem, but I'm still a little confused about the question:</p> <pre><code>select count(*) from sys.indexes; -- 697 results go select count(isnull(name,'')) from sys.indexes; -- 697 results go select count(name) from sys.indexes; -- 567 results go </code></pre> <p>And this gets the count of records for the MIN <code>name</code> field (based on collation and SQL sort order of string fields):</p> <pre><code>select i.name ,subCnt.Cnt from (select min(name) as name from sys.indexes) as i join (select name, count(*) as Cnt from sys.indexes group by name) as subCnt on subCnt.name = i.name; </code></pre> <p>And this query explains the aggregate sort order and why the above query chooses the value returned in the <code>name</code> field:</p> <pre><code>select name, row_number() over (order by name) from sys.indexes order by name; </code></pre> <p>And this query shows my collation's (Latin1_General_BIN) sort order even when replacing NULLs with char(0x7E):</p> <pre><code>select coalesce(name,char(0x7e)) , row_number() over (order by coalesce(name,char(0x7e))) from sys.indexes order by 2; </code></pre> <p>And this shows the sort order difference between collations in SQL Server (which determines what is MIN or MAX in a string field):</p> <pre><code>declare @test table (oneChar char(1) collate Latin1_General_BIN , oneChar2 char(1) collate SQL_Latin1_General_CP1_CI_AS , varb varbinary) insert into @test (oneChar) select 'c' union all select '~' union all select 'P' union all select 'X' union all select 'q' union all select NULL update @test set varb = cast(isnull(oneChar,char(0x7E)) as varbinary), oneChar2 = oneChar select min(oneChar) from @test -- 'P' select min(oneChar2) from @test -- '~' select min(varb) from @test -- 0x50, the varbinary equivalent of oneChar </code></pre> <p>And if you want the count of all rows and you want the MIN() of the name without considering the NULLs (and not seeing a warning, for whatever reason), use this:</p> <pre><code>select i1.Cnt ,i2.name from (select count(*) as Cnt from sys.indexes) as i1 ,(select min(name) as name from sys.indexes where name is not null) as i2 </code></pre> <p>And whatever you do, certainly don't cast a whole field as a different collation just to do some filtering. This question belongs in the discussion forums, not as a simple question/answer. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload