Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to convert '' (space kind of value to 0.

Posted on 2013-06-12
8
Medium Priority
?
484 Views
Last Modified: 2013-06-12
I have two fields ims and ems field where in few values are empty not null ie ems=''
How to convert empty value (i.e not null) or blank value as 0
Some where ims is empty and somewhere ems is empty

example
ims   ems   total
10               10
          20      20
10      30      40
40                40
40      50      90

The total should be ims + ems
both are of type varchar,
when i pick them i convert to int and then add
but the rows which contains empty value does nt get added up
how to make them consider as 0
0
Comment
Question by:searchsanjaysharma
  • 4
  • 4
8 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39242051
SELECT ims, ems,
    CASE WHEN ISNUMERIC(ims) = 1 THEN CONVERT(int, ims) ELSE 0 END +
    CASE WHEN ISNUMERIC(ems) = 1 THEN CONVERT(int, ems) ELSE 0 END AS [total]
FROM SomeTable
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39242055
BTW, you should not be using [n][var]char data types for this :)
0
 

Author Comment

by:searchsanjaysharma
ID: 39242110
How to use update on this.

As i ma having the statements as
update mstmassstudents set total=convert(varchar,convert(bigint,ims)+convert(bigint,ems)))
How to put it here in this context
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 39242140
>>How to use update on this.

Short answer: don't!  :)

Seriously, storing derived data like that is usually a bad idea.  But if you insist:

UPDATE mstmassstudents
SET total = CASE WHEN ISNUMERIC(ims) = 1 THEN CONVERT(int, ims) ELSE 0 END +
    CASE WHEN ISNUMERIC(ems) = 1 THEN CONVERT(int, ems) ELSE 0 END

Open in new window


If total is also varchar (again, bad idea)...

UPDATE mstmassstudents
SET total = CONVERT(varchar, 
    CASE WHEN ISNUMERIC(ims) = 1 THEN CONVERT(int, ims) ELSE 0 END +
    CASE WHEN ISNUMERIC(ems) = 1 THEN CONVERT(int, ems) ELSE 0 END)

Open in new window

0
 

Author Comment

by:searchsanjaysharma
ID: 39242152
I had to keep that as the total has exceptional data. like E, I, F etc.
So i had to keep that
 Here again the problem would be there as isnumeric will not work for values like 13.7
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 39242185
I had to keep that as the total has exceptional data. like E, I, F etc.
So i had to keep that

Please take a step back and restate your question, because it seems to me that you have not actually communicated all of the relevant requirements.

Here again the problem would be there as isnumeric will not work for values like 13.7

What do you mean by "will not work"?  ISNUMERIC('13.7') returns 1.

If the problem is that it is truncating down to 13, well, in your original question you said you were converting these values to integers.

Again, please restate your question.
0
 

Author Comment

by:searchsanjaysharma
ID: 39242222
I have resolved that, thanx.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39242224
tx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

971 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