Update composite column - sql

I just want to fill column7 of my_table with values from other columns so the result for column7-value looks like that: 86|WWB|2014 or 86|WWB|- in case that column3 has value 0. Here is my SQL:
UPDATE public.my_table
SET column7 =
case when column1 IS NULL then '-' else column1 end
|| '|' ||
case when column2 IS NULL then '-' else column2 end
|| '|' ||
case when column3 = '0' then '-' else column3 end
error: invalid input syntax for integer: "_"
The problem ist the last case-row, because column3 is integer.
Column1 and column2 are bpchar, column3 ist int2.
Is there a way to solve this problem?

You are having type collisions. It is easy to convert in Postgres:
UPDATE public.my_table
SET column7 = (coalesce(column1::text, '-') || '|' ||
coalesce(column2::text, '-') || '|' ||
(case when column3 = 0 then '-' else column3::text end)
);

Using concat will make this a lot easier to read and it automatically converts everything to text. However the case statement needs to yield the same data type for all branches, so a cast to text is still needed for column3
UPDATE public.my_table
SET column7 = concat(
coalesce(column1, '-'), '|'
coalesce(column2, '-'), '|'
case when coalesce(column3,0) = 0 then '-' else column3::text end
);

Related

creating a dynamic where clause in oracle apex form

I am creating a parameterized apex form where I take column name and its value from user via a select list.
When I use it in where clause like
select columnnames
from table
where :P592_column = :P592_value ;
It is not returning any output, but when I hardcode column name in place of :P592_column it is showing output.
You cannot use a bind variable as a dynamic column name. Instead, whitelist the columns in a CASE statement:
SELECT columnnames
FROM table
WHERE CASE UPPER( :P592_column )
WHEN 'COLUMN1' THEN column1
WHEN 'COLUMN2' THEN column2
WHEN 'COLUMN3' THEN column3
WHEN 'COLUMN4' THEN column4
END = :P592_value;
Or for multiple columns:
SELECT columnnames
FROM table
WHERE CASE UPPER( :P592_column )
WHEN 'COLUMN1' THEN column1
WHEN 'COLUMN2' THEN column2
WHEN 'COLUMN3' THEN column3
WHEN 'COLUMN4' THEN column4
END LIKE '%' || :P592_value || '%'
AND CASE UPPER( :P592_column1 )
WHEN 'COLUMN1' THEN column1
WHEN 'COLUMN2' THEN column2
WHEN 'COLUMN3' THEN column3
WHEN 'COLUMN4' THEN column4
END LIKE '%' || :P592_value1 || '%'
AND CASE UPPER( :P592_column2 )
WHEN 'COLUMN1' THEN column1
WHEN 'COLUMN2' THEN column2
WHEN 'COLUMN3' THEN column3
WHEN 'COLUMN4' THEN column4
END LIKE '%' || :P592_value2 || '%';

Get values of 2 columns as single value separated with '|'

DECLARE #mockup TABLE(Column1 VARCHAR(1),Column2 VARCHAR(1));
INSERT INTO #mockup VALUES('1','2'),('-','2'),('1','2'),('-','-'),('2','-'),('1','2');
SELECT ISNULL(NULLIF(Column1 + '|','-|'),'')
+ISNULL(NULLIF(Column2,'-'),'')
FROM #mockup
Above query result is as below,
1|2
2
1|2
2|
1|2
I want the result as above only except row4, where 2| should be only as 2 .
'|' should not be there at before or end of the values.
Simply join both fields and use REPLACE to remove |- and -|. Condition in WHERE clause avoids records where both fields are -:
select replace(replace(Column1+'|'+Column2,'-|',''),'|-','') as Result
from mockup
where coalesce(nullif(Column1,'-'),nullif(Column2,'-')) is not null
Output:
Result
1|2
2
1|2
2
1|2
See result here.
Use Replace function
SELECT replace(replace(replace(Column1 + '|' + Column2,'-|',''),'|-',''),'-','')
FROM #mockup
or try using CASE statement
SELECT CASE
WHEN column1 LIKE '[0-9]' AND column2 LIKE '[0-9]' THEN column1 + '|' + column1
WHEN column1 LIKE '[0-9]' AND column2 NOT LIKE '[0-9]' THEN column1
ELSE column2
END
FROM #mockup
if you want to check the - instead of numbers then
SELECT CASE
WHEN column1 NOT LIKE '-' AND column2 NOT LIKE '-' THEN column1 + '|' + column1
WHEN column1 NOT LIKE '-' AND column2 LIKE '-' THEN column1
ELSE column2
END
FROM #mockup
I would do this as:
select stuff( ((case when column1 not like '-' then '|' + column1 else '' end) +
(case when column2 not like '-' then '|' + column2 else '' end)
), 1, 1, '');
This is the simplest way that I've found to implement concat_ws() in SQL Server. concat_ws() is a function available in other databases, where you would just do:
select concat_ws('|',
(case when column1 not like '-' then column1 end),
(case when column2 not like '-' then column2 end)
)

Computed columns with null

I am creating a view (using MS sql 2008) with creates a calculated field as a COLUMN1 + COLUMN2. Everything is fine and dandy but: Both COLUMN1 and COLUMN2 can be NULL.
I want to follow the following rule:
2 + 2 = 4
2 + NULL = 2
NULL + 2 = 2
0 + 0 = 0
NULL + NULL = NULL
If I use ISNULL(column2, 0), then all the rules will be followed but not the last one.
How do I need to create the view
CREATE VIEW dbo.test
AS
SELECT COLUMN1, COLUMN2, (????????) AS CALCULATEDCOL FROM dbo.TabTest;
GO
create view dbo.test AS
select
column1,
column2,
case
when column1 is null and column2 is null then null
-- or when isnull(column1, column2) is null then null
else isnull(column1, 0) + isnull(column2, 0)
end as CALCULATEDCOL
from dbo.TabTest
CASE
WHEN COALESCE(COLUMN1, COLUMN2) IS NULL THEN NULL
ELSE ISNULL(COLUMN1, 0) + ISNULL(COLUMN2, 0)
END
You can do this without a case statement:
select coalesce(column1+column2,
coalesce(column1, 0) + column2,
column1 + coalesce(column2, 0)
)
(The function coalesce is equivalent to isnull, except coalesce is standard SQL and can take more than two arguments.)
There is no disadvantage to using the case statement. I'm just offering this as an alternative.
Use CASE expression instead. Like so:
CREATE VIEW dbo.test AS
SELECT
COLUMN1,
COLUMN2,
CASE
WHEN COLUMN1 IS NULL AND COLUMN2 IS NULL THEN NULL
WHEN COLUMN1 IS NULL THEN 0 + COLUMN2
WHEN COLUMN2 IS NULL THEN 0 + COLUMN1
ELSE COLUMN1 + COLUMN2
END AS CALCULATEDCOL
FROM dbo.TabTest;
GO
it is more simple than people would expect, using CASE for this question is a waste when there is a standard function called COALESCE, just replace your questionmarks with this:
COALESCE(col1 + col2, col1, col2)
You rule seems to be "Treat nulls as zero unless both fields are null, in which case, return null." I would approach this with a case, to separate the two rules:
CASE WHEN column1 is null and column2 is null then null
ELSE ISNULL(column1,0) + ISNULL(column2,0)
END
You can use a CASE to handle the last condition:
CREATE VIEW dbo.test
AS
SELECT column1,
column2,
CASE
WHEN column1 IS NULL
THEN column2 + 0
WHEN column2 IS NULL
THEN column1 + 0
WHEN column1 IS NOT NULL AND column2 IS NOT NULL
THEN column1 + column2
ELSE NULL
END AS calculatedcol
FROM dbo.tabtest;
Using a case system, you can handle when both values are null from when either of them is NULL
CREATE VIEW dbo.test
AS
SELECT COLUMN1, COLUMN2,
CASE WHEN (COLUMN1 IS NULL AND COLUMN2 IS NULL) THEN NULL
ELSE ISNULL(COLUMN1,0)+ISNULL(COLUMN2,0)
END AS CALCULATEDCOL
FROM dbo.TabTest;
GO
You can use the case keyword to achieve this.
Something on these lines
SELECT CASE
WHEN COLUMN1 IS NULL AND COLUMN2 IS NULL THEN NULL
WHEN COLUMN1 IS NULL AND... etc...

Update column if another column contains a string

I have a table that contains three columns.
column1 column2 column3
mytestdata test
myotherdata test
I want to insert 'somestring' into column3 if column1 contains the value in column2
The result would look like:
column1 column2 column3
mytestdata test 'somestring'
myotherdata test
SQL Server:
UPDATE myTable
SET column3 = 'somestring'
WHERE column1 LIKE '%' + column2 + '%'
SQL Server:
UPDATE theTable SET column3 = 'somestring'
WHERE CHARINDEX (column2, column1) > 0
You have several options, depending on the RDBMS.
For both MySQL and Oracle:
UPDATE yourTable
set column3 = 'somestring'
where INSTR(column2, column1) > 0
For SQL Server:
UPDATE yourTable
set column3 = 'somestring'
where CHARINDEX(column1, column2) > 0
You could do something like below
UPDATE tmp
SET
column3 = (CASE WHEN tmp.column1 like '%' + tmp.column2 + '%' THEN 'something' ELSE tmp.column3')
FROM
tablename tmp

SQL Server Comma Separated value among columns

I want to select columns as comma-separated values by doing something like:
select column1+','+column2+','+column3+','+coulmn4 from someTable
except if any of the columns hold null values i have to skip that column from adding comma
how to do this is SQL Server?
[All columns are of type varchar so no casting needed]
Select
Case When Len(IsNull(Column1),'') > 0 Then Column1 + ',' Else '' End,
Case When Len(IsNull(Column2),'') > 0 Then Column2 + ',' Else '' End,
Case When Len(IsNull(Column3),'') > 0 Then Column3 + ',' Else '' End,
Case When Len(IsNull(Column4),'') > 0 Then Column4 + ',' Else '' End,
Case When Len(IsNull(ColumnN),'') > 0 Then ColumnN + ',' Else '' End
From
SomeTable
try
Test table
create table #testCol (column1 varchar(10), column2 varchar(10),
column3 varchar(10), column4 varchar(10))
insert #testCol values('a', null,null,'b')
insert #testCol values(null,'a',null,'b' )
insert #testCol values(null,'a','Z','b' )
Query
select isnull(column1,'')+ case when column1 is null then '' else ',' end
+ isnull(column2,'')+ case when column2 is null then '' else ',' end
+ isnull(column3,'')+ case when column3 is null then '' else ',' end
+ isnull(column4,'')
from #testCol
Output
a,b
a,b
a,Z,b
Can you export to csv and then strip out all the double commas?
select isnull(column1 + ',', '') + isnull(column2 + ',', '') + isnull(column3 + ',', '') + isnull(coulmn4, '') from someTable