Solved

defualt values

Posted on 2011-09-07
12
195 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

18 Experts available now in Live!

Get 1:1 Help Now