How to condense multiple update statement - SQL - sql

I have an update statement for 10 columns which is replacing values in each of the column with a where condition to that specific column.
Here's the code:
UPDATE Table1
SET Col1 = REPLACE(Col1, '#DIV/0', NULL)
WHERE Col1 = '#DIV/0';
UPDATE Table1
SET Col2 = REPLACE(Col2, '#DIV/0', NULL)
WHERE Col2 = '#DIV/0';
UPDATE Table1
SET Col3 = REPLACE(Col3, '#DIV/0', NULL)
WHERE Col3 = '#DIV/0';
Like this I have it for 10 columns, it does the work but it doesn't look clear or professional.
I wanted to condense this code and make it look like a professional code.
Any suggestions is much appreciated.
Thanks!

I suggest you leave it as it is, with one change because statements such as REPLACE(Col1, '#DIV/0', NULL) do not make sense: calling REPLACE in sql server where any parameter is NULL always produces NULL, regardless if the text is found or not.
So best is this:
UPDATE Table1 SET Col1 = NULL WHERE Col1 = '#DIV/0';
UPDATE Table1 SET Col2 = NULL WHERE Col2 = '#DIV/0';
UPDATE Table1 SET Col3 = NULL WHERE Col3 = '#DIV/0';

Use case expressions keep the old value if it's not equal to '#DIV/0', the default else null will replace the '#DIV/0' values with NULL.
UPDATE Table1
SET
Col1 = case when Col1 <> '#DIV/0' then Col1 end,
Col2 = case when Col2 <> '#DIV/0' then Col2 end,
...
Note that all rows will be updated, i.e. one huge transaction. If that's a problem, either go back to your original solution with several updates, or add this at the end:
where '#DIV/0' in (Col1, Col2, ...)
If this is a good or bad idea depends on the data, how many percent of the rows/columns need to be updated. Indexes etc.

Assuming its T-SQL, why not just use NULLIF function
DECLARE ValToNull AS VARCHAR(10) = '#DIV/0'
UPDATE Table1
SET Col1 = NULLIF(Col1, ValToNull),
Col2 = NULLIF(Col2, ValToNull),
Col3 = NULLIF(Col3, ValToNull)

Related

Select where in list or else select all most elegantly

For a select statement I set a part of the where condition externally in a configuration file as parameter, like this (COL2 is filled with string values):
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 = $external_parameter
Now I have a more complex case, where I need the external parameter to be a list. Whenever a value of COL2 is in the list, it should select, however if no list is provided or some kind of "empty" tag, every value should be selected again. I came up with an idea, but it does not work that way:
SELECT COL1
,COL2
FROM TABLE1
WHERE $external_list = ('') or COL2 IN $external_list
Like this, the statement would be true for all elements of COL2 when an empty list is provided, and true for every element in the list that matches with the list if a filled list is provided.
However, if I provide a filled list it will not work on the comparison in the first half of the where statement:
('entry_a', 'entry_b') = ('')
Is there any query that would make it work - best in a one line where statement?
Try something like this,
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 IN ($EXTERNAL_PARAMETER)
OR 1 = (
CASE
WHEN $EXTERNAL_PARAMETER = ''
THEN 1
ELSE 0
END
)
Try this way
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 IN (
CASE
WHEN $EXTERNAL_PARAMETER = ''
THEN COL2
ELSE $EXTERNAL_PARAMETER
END)
Try this
IF $EXTERNAL_PARAMETER = ''
SET $EXTERNAL_PARAMETER = NULL
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 = ISNULL($EXTERNAL_PARAMETER, COL2 )

SQL- changing all fields in a table with a specific string to NULL

I need to change (mutate) all fields with with value 'N/A' to NULL for all records in a table - is there a quick way to accomplish this ?
You can perform simple UPDATE operation like this.
UPDATE table_name SET string_column = NULL WHERE string_column = 'N/A'
To avoid errors, column must be nullable (i.e don't have 'NOT NULL' ih definition).
This is possible to update all columns by one query like this.
UPDATE table_name SET
col1 = CASE
WHEN col1 = 'N/A' THEN NULL
ELSE col1
END,
col2 = CASE
WHEN col2 = 'N/A' THEN NULL
ELSE col2
END

remove first comma in string using sql

my string like that :
1 ,QCIM1J25836, QCIM1J27637
2 ,QCIM1J25836, QCIM1J27637, QCIM1J27638
I want to remove first comma only it means my output will be for
1 QCIM1J25836, QCIM1J27637
2 QCIM1J25836, QCIM1J27637, QCIM1J27638
may be in other will not be comma...so please tell me how can I update all data like that...
The following query should work:
It will update all the records in the column.
UPDATE table
SET col2 = CASE WHEN LEFT(col2 ,1) =',' THEN RIGHT(col2,LEN(col2)-1)
ELSE col2 END
UPDATE table
SET col2 = case when charindex(',',col2,0) =1 then right(col2, len(col2)-1) else col2 end
In SQL-Server you can do It in multiple ways.
You use STUFF in following:
SELECT col1,
STUFF(col2,1,1,'') as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%'
or you can use RIGHT
SELECT col1,
RIGHT(col2,LEN(col2)-1) as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%';
or you can use SUBSTRING
SELECT col1,
SUBSTRING(col2, 2, 255) as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%';
UPDATE
As per your comment you can update in the same ways too:
Using SUBSTRING
UPDATE tbl
SET col2 = SUBSTRING(col2, 2, 255)
WHERE col2 LIKE ',%';
Or using RIGHT
UPDATE tbl
SET col2 = RIGHT(col2,LEN(col2)-1)
WHERE col2 LIKE ',%';
Or using STUFF
UPDATE tbl
SET col2 = STUFF(col2,1,1,'')
WHERE col2 LIKE ',%';
USE [LIB]
CREATE FUNCTION [dbo].[trimChar]
(
#p_string varchar(max),
#p_char varchar(1)
)
RETURNS varchar(max)
AS
BEGIN
declare #l_string varchar(max) = #p_string
-- lets do the front
while SUBSTRING(#l_string,1,1) = #p_char
begin
set #l_string = substring(#l_string,2,len(#l_string))
end
-- lets do the back
set #l_string = reverse(#l_string)
while SUBSTRING(#l_string,1,1) = #p_char
begin
set #l_string = substring(#l_string,2,len(#l_string))
end
set #l_string = reverse(#l_string)
return #l_string
END
GO

Stored procedure setting a value if row is empty

How would I go about setting a value if a row is empty ('')?
I was thinking something like,
Got var with default value called #defaultValue to set it where the row in a table is ''.
if (select col1 from table1 where col1 = '')
set (select col1 from table1 where col1 = '') = #DefaultValue
is there a better way?
code is just a draft its not even tested..
If you want to update the table with #DefaultValue, you can use WHERE clause in the UPDATE query:
UPDATE table1
SET col1=#DefaultValue
WHERE col1=''
OR col1 IS NULL
OR
If you are trying to select #DefaultValue if the column is empty or null, you can do this:
SELECT CASE WHEN (col1 IS NULL OR col1='')
THEN #DefaultValue
ELSE col1
END AS Col1
FROM table1
select case when col1 ='' then #DefaultValues else col1 end from table
DEMO
declare #default int
set #default=1
declare #tbl table(col1 int)
insert into #tbl values(1),(''),(2)
select case when col1='' or col1 is null then #default else col1 end from #tbl

SQL If parameter is null search all

I have a JSF page where users can search by five options, and can choose how many of those options they search by (0-5). The database has no NULL values.
Currently I have a horrendous piece of code that builds that SQL statement, but I would like to just have one parameterized statement.
SELECT *
FROM table1
WHERE col1 = 'dave'
AND col2 = 'Smith'
AND col3 = '3/2/2014'
AND col4 = '12345'
AND col5 = '67890'
ORDER BY col5 DESC
Is there a way to detect an empty/null value in the search criteria e.g col5=''
And just return all results for that column?
So that if the search criteria was just col1='dave' then the statment would act as if the input was
SELECT *
FROM table1
WHERE col1 = 'dave'
ORDER BY col5 DESC
You can do this in SQL:
SELECT *
FROM table1
WHERE (col1 = #col1 or #col1 is null) and
(col2 = #col2 or #col2 is null) and
. . .
ORDER BY col5 DESC;
But, you may not want to. If you are using indexes to speed your searches, then the use of or can impede the use of the indexes in many databases (but apparently not in Oracle). This is also true of coalesce(), which could be used as:
WHERE col1 = coalesce(#col1, col1) and
col2 = coalesce(#col2, col2) and
. . .
If you are constructing the query anyway, then do it in the statement. The logic is like:
if #col1 is not null then
#where = concat(#where, " and col1 = '", #col1, "'")
end if;
if #col2 is not null then
#where = concat(#where, " and col2 = '", #col2, "'")
end if;
This will construct the correct where clause that can use available indexes.
There is one downside to this approach. You cannot pre-compile the statement for all parameter values. The statement would need to be recreated every time new values are input (this would normally be an acceptable amount of overhead because you are dealing with user input).
Try to use trim to change empty value '' to null and then use coalesce to replace null with value from col5 as below
SELECT *
FROM table1
WHERE col1 = 'dave'
AND col2 = 'Smith'
AND col3 = '3/2/2014'
AND col4 = '12345'
AND col5 = coalesce(trim('67890'),col5)
ORDER BY col5 DESC
SELECT *
FROM table1
WHERE (col1 = :col1 OR :col1 IS NULL)
AND (col2 = :col2 OR :col2 IS NULL)
AND (col3 = :col3 OR :col3 IS NULL)
AND (col4 = :col4 OR :col4 IS NULL)
AND (col5 = :col5 OR :col5 IS NULL)
ORDER BY col5 DESC
It is more convenient to use named parameter instead of "?" in JAVA