• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Add/Assign value to non-null text values

Explorer ,
Jun 15, 2009 Jun 15, 2009

Copy link to clipboard

Copied

I have a database which stores values (schedules) for each day of the month - such as whether a person is OFF or on VAC (vacation).  So, it would look something like this:

NameMonTueWed
John SmithOFFOFF
Charlie MurphyTRN
Ricky JamesVACVACVAC
TOTAL231

So, I need the "TOTAL" row to add up the columns where the value is non-null.  I was thinking that perhaps I could somehow assign a value of 1 to each in the columns, and then perform a sum.

Can anyone assist in a Coldfusion solution or SQL Server 2005 solution?  Thanks.

TOPICS
Advanced techniques

Views

926

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Jun 16, 2009 Jun 16, 2009

I was thinking that perhaps I could somehow assign a value of 1 to each in the columns, and then perform a sum.

Yes, you could use a CASE statement to return 1 or 0.  Then wrap it in a SUM(..).

      SELECT SUM( CASE WHEN ColumnName IS NULL THEN 0 ELSE 1 END ) AS TotalNumber ....

Votes

Translate

Translate
Enthusiast ,
Jun 16, 2009 Jun 16, 2009

Copy link to clipboard

Copied

select count(Mon) as total_mon, count(Tue) as total_tue, count(Wed) as total_wed
from table
group by name

Mack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 16, 2009 Jun 16, 2009

Copy link to clipboard

Copied

Mack,

That doesn't seem to give an accurate answer.  Count(mon) just seems to return the number for all rows in the column.  So, in this case for Mon, instead of a count of 2 being returned (1 for OFF and 1 for VAC), it returns 3, as there are 3 rows.  Any ideas?  Anyone?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 17, 2009 Jun 17, 2009

Copy link to clipboard

Copied

Hmm, are you sure you have NULL values there ? (maybe you have empty

strings instead of NULL)

From the documentation

(http://msdn.microsoft.com/en-us/library/ms175997.aspx) : Count(mon)

is equivalent to Count(ALL mon) and it counts the number of non-null

rows.

Mack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 29, 2009 Jun 29, 2009

Copy link to clipboard

Copied

LATEST

You were right Mack.  I did have empty strings in the database.  I did modify the other answer given as so, which solved the problem:

SUM(CASE WHEN column = '' THEN 0 ELSE -1 END)

Thanks to both of you for your help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 16, 2009 Jun 16, 2009

Copy link to clipboard

Copied

I was thinking that perhaps I could somehow assign a value of 1 to each in the columns, and then perform a sum.

Yes, you could use a CASE statement to return 1 or 0.  Then wrap it in a SUM(..).

      SELECT SUM( CASE WHEN ColumnName IS NULL THEN 0 ELSE 1 END ) AS TotalNumber ....

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation