Replacing Null to Blank in multiple rows - sql-server

I have multiple null values in a table including different rows and columns, how I can replace them with one query like using 'all" or *, wherever the Null values in the whole table?

Check if column value is null,if yes update with blank values like below code
Update TableName
SET TableName.Column=''
WHERE TableName.Column IS NULL
For Multiple Columns
UPDATE TableName
SET col1 = COALESCE(col1,''),
col2 = COALESCE(col2,''),
col3 = ...
Coln = COALESCE(coln,'')

Related

Check null parameter in IN Clause

I have a stored procedure with a few parameters. One of them is a varchar containing a possible list of IDs (comma separated values i.e. 1, 2, 5, 9). I need the procedure to ignore the parameter when it is NULL. This is the way I actually do:
CREATE PROCEDURE PROC_TEST
#MAIN_ID INT = NULL,
#DETAIL_IDs VARCHAR(2000)
AS
BEGIN
SELECT ..... FROM TABLE1 INNER JOIN TABLE2 ON ...
WHERE
TABLE1.ID = ISNULL(#MAIN_ID, TABLE1.ID) AND
(
#DETAIL_IDs IS NULL
OR TABLE2.ID IN
(SELECT VALUE FROM STRING_SPLIT(#DETAIL_IDs,','))
)
END
It seems like the #DETAIL_IDs IS NULL part of the code requires a lot of time to execute: What am I doing wrong here?
Even if I remove the OR clause and simply add AND #DETAIL:IDs IS NULL it takes a long time.
The tables have more than 1 million records each.

Merge statement to delete/update/insert data in Sql Server

I have an excel file with thousand of rows which I need to use to delete/update/insert some tables.
The excel provides the following data: provider_id, country_name, locale, property1, property2.
The tables which need to be updated are:
provider_country with columns : provider_country_id, provider_id, country_id, property1, property2 and
provider_country_language with columns : provider_country_language_id, provider_country_id, language_id.
I can also use table country with columns (for joins): country_id, country_name.
And table language with columns: language_id, locale, country_id.
The fields which need to be updated are country_id, language_id, property1,property2 (from provider_country and provider_country_language)
I have created a temporary table with all the data from the excel:
CREATE TABLE #TempProviderCountryLanguage(
[provider_id] int NULL,
[country_name] nvarchar(50) NULL,
[locale] nvarchar(10) NULL,
[property1] int NULL,
[property2] decimal(5,2) NULL
)
INSERT INTO #TempProviderCountryLanguage VALUES
(1,N'Provider1',N'Brazil',N'en-br',4,NULL)
INSERT INTO #TempProviderCountryLanguage VALUES
(1,N'Provider1',N'Brazil',N'pt-br',4,NULL)
INSERT INTO #TempProviderCountryLanguage VALUES
(1,N'Provider1',N'Denmark',N'da-dk',4,12.21)
INSERT INTO #TempProviderCountryLanguage VALUES
(2,N'Provider2',N'Denmark',N'da-dk',5,14.21)
......
MERGE [provider_country] AS TARGET
USING (
SELECT tb.provider_id
,c.country_id
,l.language_id
,tb.property1
,tb.property2
FROM #TempProviderCountryLanguage tb
INNER JOIN country c ON c.country_name = tb.country_name
INNER JOIN language l ON l.locale = l.locale
) AS SOURCE
ON (
TARGET.provider_id = SOURCE.provider_id AND
TARGET.country_id = SOURCE.country_id
)
WHEN MATCHED
THEN
UPDATE
SET TARGET.country_id = SOURCE.country_id,
TARGET.property1 = SOURCE.property1,
TARGET.property2 = SOURCE.property2
WHEN NOT MATCHED BY TARGET
THEN
INSERT (
provider_id
,country_id
,property1
,property2
)
VALUES (
SOURCE.provider_id
,SOURCE.country_id
,SOURCE.property1
,SOURCE.property2
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
For the provider_country_language I plan to make another merge.
I am trying to update the tables using merge but I have a problem because I cannot make a unique select here (somehow I would need the language_id as well):
ON (
TARGET.provider_id = SOURCE.provider_id AND
TARGET.country_id = SOURCE.country_id
)
And the error is :
The MERGE statement attempted to UPDATE or DELETE the same row more
than once. This happens when a target row matches more than one source
row. A MERGE statement cannot UPDATE/DELETE the same row of the target
table multiple times. Refine the ON clause to ensure a target row
matches at most one source row, or use the GROUP BY clause to group
the source rows.
How can I make this work and make sure all the tables are updated correctly?(not necessarily using Merge)
And from performance point of view what would be the best approach? (only the INSERT INTO will be performed thousand of times...)

Columns with Null Values in SQL Server vs Oracle

In Oracle: ='' and 'is null' return NULL Values
SELECT COL1 FROM TABLE WHERE COL1='' --> RETURN COL1 WITH NULL VALUES
SELECT COL1 FROM TABLE WHERE COL1 IS NULL --> RETURN COL1 WITH NULL VALUES
Both queries return the same set of rows.
In Microsoft SQL:
SELECT COL1 FROM TABLE WHERE COL1='' --> RETURN COL1 WITH <BLANK> VALUES
SELECT COL1 FROM TABLE WHERE COL1 IS NULL --> RETURN COL1 WITH <NULL> VALUES
Here the first and second queries return different sets of rows.
Why is there a difference between the Microsoft SQL result set and the Oracle result set?
How could I retrieve both set of rows in Microsoft SQL? (Values which are '' and values which are NULL).
In SQL Server '' is not null. In Oracle its a bit complicated x char := '' is not null in PL/SQL but as varchar2 empty string '' is exactly same as null as explained here And in case of queries this is varchar2 so condition COL = '' is never true.
If you need condition that will work in both DBs please use coalesce function.
select * from TAB
where coalesce(COL,'SOME_UNIQUE_STRING') = coalesce(FILTER_PARAM, 'SOME_UNIQUE_STRING')
Such condition should return rows when COLUMN is equal to FILTER as well as both are null or empty strings.
To retrieve both sets of data, you could use isnull.
SELECT COL1 FROM TABLE WHERE ISNULL(COL1, '') = ''
Oracle automatically converts empty strings to null values, and sql server does not, which is why the result sets are different.
You can find more information on this here:
Why does Oracle 9i treat an empty string as NULL?
To avoid defining a unique string, you could use a case statement.
This way we evaluate col when null or empty set to the same value for comparison. Since case to my knowledge is DBMS independent this should work in both.
SELECT *
FROM Table
WHERE case when col is NULL or col = '' then 1 else 0 end = 1

SQL Server : how to update and replace with wildcards

How exactly do I replace something by using wildcard characters?
I had tried with this but it doesn't work
UPDATE [dbo].[test]
SET [Fælge] = REPLACE([Fælge],'%ET%%%','')
I want all the ET** to be blank
My data looks like this and it goes from 4-12x10-24 ET0-99 plus a half like(4.5x13 ET35.5)
6x15 ET0|6.5x16 ET55|6x16 ET50|7x17 ET60|7x17 ET65
my data grouped and counted
i want both ET and the numbers to be blank so the data just looks like
6x15 |6.5x16 |6x16 |7x17 |7x17
I will assume that the sample data provided is indicating multiple rows. You can do this quite easily using LEFT and CHARINDEX.
if OBJECT_ID('tempdb..#Something') is not null
drop table #Something
create table #Something
(
SomeValue varchar(50)
)
insert #Something(SomeValue) values
('6x15 ET0'),
('6.5x16 ET55'),
('6x16 ET50'),
('7x17 ET60'),
('7x17 ET65')
update #Something
set SomeValue = rtrim(LEFT(SomeValue, CHARINDEX('ET', SomeValue) - 1))
select *
from #Something

SQL Server Get Updated Column Value

I have a col1 in my table with initial value of 0 with one row, I want to get updated value of col1.
My update query is :
Update table set col1 = col1 + 1
I can get last update by put output like :
Update table set col1 = col1 + 1 OUTPUT inserted.col1
But I am not sure that output value is related same query or last updated by other query at same time.
The value returned is the value of this update statement. It will not reflect updates made by other users.
EDIT:
You can also store the value in a variable without an OUTPUT clause (https://msdn.microsoft.com/en-us/library/ms177523.aspx):
Update table set #col1 = col1 = col1 + 1;

Resources