Solved

defualt values

Posted on 2011-09-07
12
199 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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