Solved

defualt values

Posted on 2011-09-07
12
197 Views
Last Modified: 2012-05-12
I need to create a stored procedure in the following format:
INSERT INTO dbo.Table(
       Row1,
      Row2,
      Row3,
etc


Select Row1,
      Row2,
      Row3,
etc

FROM SomeTable

--- But ---

Row1 for example in the select might be null --- how would i for example insert a 0 in that case in the insert. (row one is a numeric field).
0
Comment
Question by:vbnetcoder
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36495987
insert into INSERT INTO dbo.Table(
       Row1,
      Row2,
      Row3,
...
...
)

Select CASE WHEN Row1 is null then 0 else end as Row1,
      Row2,
      Row3,

...
...
From sometable
0
 
LVL 5

Expert Comment

by:eridanix
ID: 36496004
INSERT INTO dbo.Table(
       Row1,
      Row2,
      Row3,
etc.


SELECT CASE WHEN Row1 is null THEN 0 ELSE Row1 END AS Row1,
      Row2,
      Row3,
etc.

FROM SomeTable
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 250 total points
ID: 36496036
insert into INSERT INTO dbo.Table(
       Row1,
      Row2,
      Row3,
...
...
)

Select CASE WHEN Row1 is null then 0 else Row1 end as Row1,
      Row2,
      Row3,

...
...
From sometable

Open in new window


just a slight modification.

You can perform same case statement on all of them.

You can also use COALESCE

Select COALESCE (Row1, NULL, 0) FROM SOMETABLE, ETC


0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 36496351
Can you just use ISNULL?

ISNULL(Row1, 0) as Row1

Open in new window


It will return Row1 unless it's null, then it will return 0
0
 

Author Comment

by:vbnetcoder
ID: 36496369
Select COALESCE (Row1, NULL, 0) FROM SOMETABLE, ETC worked for me.

Is it better or worse then using select case?

0
 

Author Comment

by:vbnetcoder
ID: 36496410
I am doing this to a date

COALESCE (CONVERT(VARCHAR(10), MC.MyDate, 101, NULL, 0)

How would i update to make empty string for null?
0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 250 total points
ID: 36496471
Your second value in COALESCE (the NULL one) is unnecessary - you could achieve the same thing by removing it:

COALESCE (Row1, 0) 

Open in new window


COALESCE is the exact same thing as ISNULL, but with more parameters - it just goes from parameter 1 to 2 to 3 and so on, until it finds one that's not NULL, and then it returns that. To return an empty string instead of zero, you'd change it to this:

COALESCE (Row1, '') 

Open in new window

0
 

Author Comment

by:vbnetcoder
ID: 36496496
Sorry

I am doing this currently

CONVERT(VARCHAR(10), MC.CoverageEffectiveDt, 101)

Now, i want to use COALESCE if null is returned
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36496543
select coalesce(CONVERT(VARCHAR(10), MC.CoverageEffectiveDt, 101),null,'') from yourtable
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36496569

@ryanmccauley is right though. It doesn't hurt anything at all but doesn't need to be there.

either above or this will suffice

select coalesce(CONVERT(VARCHAR(10), MC.CoverageEffectiveDt, 101),'') from yourtable
0
 

Author Closing Comment

by:vbnetcoder
ID: 36496599
ty
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 36496655
you are very welcome
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now