Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

defualt values

Posted on 2011-09-07
12
198 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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