Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 26th, 2006, 04:32 AM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Suppress zeros from displaying

I'm using Transact-SQL to provide a data source file for a text formatter/distributer which unfortunately doesn't have zero suppression. My output has 6 columns of numbers and each row only has values in 2 of those columns (it's an aged debtors report). I don't want the 0.00 to show in the other columns.
I tried this:
  isnull(cast(arage1 as char(15)),'') "arage1"
where column arage1 is originally decimal(16,2). But that still shows 0.00 in the output.

Any ideas please?
 
Old October 26th, 2006, 05:30 AM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Doh! of course "isnull" won't work, the field isn't null - it's 0.00.
So, I want to do something like this:
   case when arage1=0 then'' else arage1 end
but of course it can't convert numeric to character. How do I tell it to do that? Thanks, Sharon.
 
Old October 26th, 2006, 06:12 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What you're trying to do is poor practice, the data should be "pure", formatting etc. should be the purview of the client. If you want you can create a temporary table with NVARCHAR fields and insert your data into those using appropriate CASE expressions, CASE 0 THEN '' ELSE arage1.

--

Joe (Microsoft MVP - XML)
 
Old October 26th, 2006, 07:30 AM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. Yes, I'm trying to get around a limitation in the client by being clever in my SQL output.
I've tried:
   case when arage1=0 then replace(cast(arage1 as nvarchar),'0','') else arage1 end,
but get error converting data type nvarchar to numeric.
I've also tried:
   case when arage1=0 then replace(cast(arage1 as nvarchar(1)),'0','') else arage1 end,
but get an arithmetic overflow.
However, when I do as you suggest and put the nvarchar results into a temp table, then case when those results, I do get my blanks.
Thanks so much, I was flailing with cast/convert/replace but this answer did solve my problem.
 
Old October 26th, 2006, 07:32 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Sharon,

This should work... and without a CASE or a Temp Table...

ISNULL(NULLIF(columnname,0),' ')

--Jeff Moden
 
Old October 26th, 2006, 09:17 AM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff - I don't know about NULLIF yet. But I tried that syntax and got "Error converting data type varchar to numeric."
     select dw_nadr.mmatter, ISNULL(NULLIF(dw_nadr.arage1,0),' ') from dw_nadr
The column dw_nadr.arage1 is decimal(16,2) so I'm confused by the error message.
 
Old October 26th, 2006, 06:27 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Shoot... my bad, Sharon... implicit conversion hierarchy got me... this will do the trick with the added benefit of lining up your numbers on the decimal point...

SELECT dw_nadr.mmatter, ISNULL(STR(NULLIF(dw_nadr.arage1,0),16,2),' ') AS ArAge1
FROM dw_nadr.

--Jeff Moden
 
Old October 27th, 2006, 04:05 AM
Authorized User
 
Join Date: Oct 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are a dream Jeff! That works great. I'll study the STR and NULLIF functions some more and I promise not to bother you with an easy one again.
 
Old October 27th, 2006, 08:12 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Thanks Sharon,

By the way... if you don't know the answer to a question or which tools to look for, even seemingly simple problems are flat impossible. That's why folks started forums like this one. Never be afraid to ask even the "simple" questions if you've looked for but can't find the answer.

Thanks for taking the time to lookup the two functions... now, THAT is an excellent move.

Just as a kick start on those two... STR converts a number to a right justified number (left padded with spaces) and takes 3 operands...

STR(stringtoconvert,totalfinalwidth,numberofdecima lplaces)

I usually don't trust defaults but the 2nd operand defaults to 10 and the 3rd operand defaults to 0. Here's an "oolie" and I don't know if you'll ever need it and it's undocumented but if you ever need to do an odd thing called "Bankers Rounding", the STR function is the only thing in SQL Server that will do it.

The NULLIF function takes two operands...

NULLIF(thingtocheck,conditiontoreturnnull)

What it does is it checks the first operand against the second... if they match, the function returns NULL... if they don't match, the function returns the first operand. The following two code snippets are functionally equivelant...

NULLIF(SomeColumn,0)

CASE
  WHEN SomeColumn = 0
  THEN NULL
  ELSE SomeColumn
END

Write back if you have any additional questions...

"We're all in this together and I'm pullin' for ya" - Red Green

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
help with finding zeros of ! ludasky Visual Basic 2005 Basics 1 May 1st, 2008 04:08 PM
Remove Leading and Trailing Zeros lene_wee SQL Server 2000 3 April 1st, 2008 08:23 AM
Padding Zeros jmss66 Classic ASP Basics 5 January 28th, 2008 07:38 PM
Zeros at left... comicghozt Access 7 October 3rd, 2006 02:27 AM
.ToString() functions cuts off zeros olambe BOOK: ASP.NET Website Programming Problem-Design-Solution 2 July 6th, 2004 10:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.