Solved

Substitute a value for zero in SQL Server

Posted on 2009-04-13
3
220 Views
Last Modified: 2012-05-06
I have a list of products, some of which contain a default weight and some of which do not.
I need to create a list where this weight is used as a multiplier. I can't chenge the data as this would cause problmes elsewhere.

What I am looking for is something like COALESCE(BoxWeight, 25) but which would work for a zero value rather that a Null value as is the case for COALESCE

Any ideas?
0
Comment
Question by:grwallace
3 Comments
 
LVL 11

Assisted Solution

by:bmatumbura
bmatumbura earned 150 total points
ID: 24129281
CASE WHEN BoxWeight = 0 Then 25 ELSE BoxWeight END
0
 
LVL 12

Assisted Solution

by:Dimitris
Dimitris earned 150 total points
ID: 24129287
use case
for example :

SELECT CASE WHEN ISNULL(BoxWeight,0 ) =0 THEN 25 * something
                       ELSE BoxWeight * something
             END as Result
FROM My_Table
0
 
LVL 1

Accepted Solution

by:
joshbenner earned 200 total points
ID: 24129302
Use a CASE expression.

See: http://msdn.microsoft.com/en-us/library/ms181765(SQL.90).aspx

Example:

SELECT
  ProductName,
  Weight,
  WeightMultiplier = CASE BoxWeight WHEN 0 THEN 25 ELSE BoxWeight
FROM
  Products
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question