Convert Stored procedure result from NULL to 0 - sql

I have temp table accepting result set of executing stored proc. I can't change stored proc. The sp return null columns in some rows but I want to convert NULL to 0 when insert them into the temp table. How to do it easily?
My SQL is like:
Insert into #temp (co1, co2, co3)
exec sp_xxx
co3 from sp_xxx might be NULL, but I want to convert it to 0 and store in #temp.col3

Give it a try,
ISNULL(COLUMN_NAME, 0)

Insert into #temp (co1, co2, co3);
exec sp_xxx;
-- update value where column have null value :
update #temp set col1=isnull(col1,0), col2=isnull(col2,0), col3=isnull(col3,0)
where (col1 is null) or (col2 is null) or (col3 is null)

You can create temp table with 3 more comuputed columns value of which is based on your current columns..
Example below..
CREATE TABLE #Products
(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
, InventoryValue AS isnull(QtyAvailable,0)
)
insert into #Products(QtyAvailable,UnitPrice)
values (null,10),(20,10)
select * from #Products
In the above example InventoryValue is the computed column and value is populated based on QtyAvailable value..
Hope it helps!

As Vikram says, after retrieving the data into your temp table you can then update the values accordingly. if it is just column3 that could be null then
UPDATE #temp
SET col3 = 0
WHERE col3 IS NULL
will do this just fine.
Otherwise you could just do the ISNULL check when you select back from your temp table and use the information later on
SELECT ISNULL(col3, 0)
FROM #temp

Related

Update null values when we use Addition assignment (+=)

declare #temp table
(
Id int,
Qty int
)
insert into #temp values(1,null)
update #temp set Qty+=2
select * from #temp
I am trying to update the Qty column however the Qty column is null by default. So because the production database column might be null when I try to update I am getting a null value I need Qty to be 2.
Use coalesce (or isnull) to handle the null case.
update #temp set Qty = coalesce(Qty,0) + 2;
If you want to increment the value, then the column should not allow NULLs.
I would suggest that you default the value to 0. Then don't insert a NULL value explicitly:
declare #temp table (
Id int,
Qty int not null default 0
);
insert into #temp (id) values (1);
update #temp set Qty += 2;
select * from #temp;
You can also express the insert as:
insert into #temp (id, value) values (1, default);

isnull function does not return correct

When I am using isnull it does not return the '' please see below I have original DOB, isnull used, cast as date.
You would need to convert dob to a char/nchar/varchar/nvarchar type to use isnull() or coalesce() like that.
select isnull(convert(varchar(10),dob,120),'')
if you really would like to return an empty string for the date value, you could try this in a new query window. It creates a table to repoduce your requirement of a null date value and then selects the value before dropping the table.
CREATE TABLE dbo.Test
(
Id INT IDENTITY(1,1) NOT NULL
,Date1 DATE NULL
)
INSERT INTO dbo.Test(Date1) VALUES ('01/01/2017')
INSERT INTO dbo.Test(Date1) VALUES ('01/02/2017')
INSERT INTO dbo.Test(Date1) VALUES (NULL)
INSERT INTO dbo.Test(Date1) VALUES ('01/04/2017')
SELECT * FROM dbo.Test
SELECT Date1 = CASE WHEN date1 IS NULL THEN '' ELSE CAST(DATE1 AS VARCHAR(10)) END from dbo.Test
DROP TABLE dbo.Test
go

INSERT ONLY SPECIFIC COLUMN FROM A STORED PROCEDURE RESULT

I want to know if it is possible to insert to a table from a specific column of result from a stored procedure?
Something like:
declare #temp as table(
id int
)
insert #temp
exec getlistofStudents --returns multiple columns
this is an example only, Thanks for the help..
You can take a 2 step approach. First INSERT INTO a #TempTable, then populate the #TempVariable with another INSERT INTO, selecting the single column.
DECLARE #temp AS TABLE
(
ID int
);
CREATE TABLE #tempTable1
(
Column1 int,
Column2 int
);
INSERT INTO #tempTable1
Exec getlistofStudents
INSERT INTO #temp
SELECT Column1 FROM #tempTable1

SQl Constraint UNIQUE based on other column value

Table A has columns 1 and 2.
Column 1's value must be unique if column 2 is equal to x.
ALTER TABLE A
ADD UNIQUE (1) WHERE 2 = x.
But this gives me a syntax error near WHERE.
I tried to create an index, but I can't figure out how to make that do what I want either.
Create unique nonclustered index [my_index]
on [TableA]([1])
where [2] = x
Here's an alternative solution using a Function although the index is a better solution:
CREATE FUNCTION dbo.CheckConstraint
(
#col1 int,
#col2 CHAR(1)
)
RETURNS INT
AS
BEGIN
DECLARE #ret INT
SELECT #ret = COUNT(*)
FROM YourTable
WHERE col1 = #col1 AND #col2 = 'X'
RETURN #ret
END;
CREATE TABLE YourTable (col1 int, col2 char(1));
ALTER TABLE YourTable
ADD CONSTRAINT CheckForXConstraint CHECK (NOT (dbo.CheckConstraint(col1,col2) > 1));
INSERT INTO YourTable VALUES (1, 'X');
INSERT INTO YourTable VALUES (2, 'X');
INSERT INTO YourTable VALUES (2, 'Y');
INSERT INTO YourTable VALUES (2, 'X'); <-- This line fails
SQL Fiddle Demo

How to allow temporary tables to accept null values

If you create temp tables using "insert into" in SQL Server it uses the first insert to determine whether a column accepts null value or not. if the first insert has null value the column become nullable otherwise it will be non-nullable.
Is there a way to create temp tables using "insert into" to accept null values?
Example
This works without any problem
Select 'one' as a , null as b
into #temp
insert into #temp
Select 'two' as a , 500 as b
However this throws "Cannot insert the value NULL into column 'b'"
Select 'one' as a , 500 as b
into #temp
insert into #temp
Select 'two' as a , null as b
I know I could do create Table or alter column statement but I want to do it without rewriting hundreds of the existing queries.
How about this?
Select CONVERT(varchar(100), 'one') as a , CONVERT(int, 500) as b
into #temp
insert into #temp
Select 'two' as a , null as b
select * from #temp order by 1
I would workaround this by explicitly creating temporary table before first insert.
create table #temp (a varchar(10) not null, b int null)
(Un)fortunately, this question is too popular and appears at the top for Sybase ASE 15.7 as well, so just adding my answer for Sybase here.
For me neither of cast, convert or coalesce worked, but a case statement did (which is what coalesce is, but eh...)
select
a = case when 1 = 0 then null else 'one' end,
b = case when 1 = 0 null else 500 end
into #temp
This is an old question but I had a similar issue where I UNION NULLs to the initial query which may have helped the OP.
Select 'one' as a , 500 as b
into #temp
UNION
SELECT NULL, NULL
insert into #temp
Select 'two' as a , NULL as b
Putting it here so the next time I need to do this and forget how...