Concat in update - sql

I have the following table
ID Col1 Col2 Col3
1 A B NULL
2 A B NULL
3 A B NULL
I am trying to concat col1 and col2 in my third column.
I tried this :
update TABLE set Col3 = concat(col1, '', Col2);
But SQL Server told me that concat is unknown as an integrated function.
How can I do this?

You can use the concatenation operator (the '+' sign) like this:
UPDATE TABLE SET Col3 = Col1 + Col2

Just use the + sign to concatenate. update TABLE set Col3 = col1 + ''+ Col2.
IF you are concatenating integers , convert them to varchar first.
UPDATE TABLE
SET [Col3] = CONVERT (VARCHAR(10),[col1]) + ' ' + CONVERT(VARCHAR(10),[Col2])

Related

Storing and Query Blank Values in Hive Columns

I have a requirement for storing blank strings of length 1, 2, and 3 in some columns of my Hive table.
Storing:
If my column type is char, then I see that the data is always trimmed before storing. i.e. length(column) is always 0
If my column type is varchar then the data is not trimmed. so length(column) is 1, 2 and 3 respectively.
So that solves my storing problem.
Querying:
I am unable to query the column by value.
say. select * from hive table where column = ' ';
it only works if I do something like
select * from hive table where length(column) > 0 and trim(column) = '';
Is there a way to handle this separately ?
say I want to query those records where column value is of a blank string of length 3? How do I do this?
This is what i Tried (Note that the issues seems to be when the file is stored as parquet)
CREATE EXTERNAL TABLE IF NOT EXISTS DUMMY5 (
col1 varchar(3))
STORED AS PARQUET
LOCATION "/DUMMY5";
insert into DUMMY5 values (' '); // 2 character strings
insert into DUMMY5 values (' '); //3 character strings
select col1, length(col1) from DUMMY5;
+-------+------+--+
| col1 | _c1 |
+-------+------+--+
| | 3 |
| | 2 |
+-------+------+--+
select col1, length(col1) from DUMMY5 where col1 = ' '; // 0 record
select col1, length(col1) from DUMMY5 where col1 = ' '; // 0 record
Running Hive 2.1.1
drop table dummy_tbl;
CREATE TABLE dummy_tbl (
col1 char(1),
col2 varchar(1),
col3 char(3),
col4 varchar(3)) ;
insert into dummy_tbl values (' ', ' ', ' ', ' ');
select length(col1), length(col2), length(col3), length(col4) from dummy_tbl;
Result:
c0 c1 c2 c3
0 1 0 2
Varchar column works absolutely correct. col2 was trimmed on insert, it is documented.
col4 varchar(2) works correctly, this query returns 1:
select count(*) from dummy_tbl where col4=' '; --returns 1
And length of all char columns shows 0 and comparison ignoring spaces like it is documented:
select count(*) from dummy_tbl where col1=' '; --single space --returns 1
select count(*) from dummy_tbl where col1=' '; --two spaces --also returns 1 because it is ignoring spaces
You can use varchar with proper length. Or STRING type if you not sure about length.

SQL script to fill in two columns with the data of another one

I need your help to tidy a table. I added two new columns. I would like to fill in these columns(col2, col3) with the data of another one (col1). At the moment, this column contain two values separate by a "val1;val2".
col1 contains a string but sometimes val1 doesn't exist and col1 contains only ";val2"
For every row of this table, I want to split the value in the col1 to separate the date execute this code:
If(val1 != null) col3 = 1
col2 = val2
col1 = val1
Finally, every column contains only one value.
Before the script:
col1 (string) = "tom;car"
After the script:
col1 (string) = "tom"
col2 (string) = "car"
col3 (bit) = "1"
I don't know how to do this directly in SQL Server Management. I would like to create a script to execute this code.
Something like this?
update t
set col3 = (case when col1 not like ';%' then 1 else col3 end),
col1 = left(col1, charindex(';', col1) - 1),
col2 = substring(col1, charindex(';', col1) + 1, len(col1));

SQL -Find and Replace the value in the table

I have a table in my database. It contains 35 columns and 150 rows. Some of its values are 0. How can I replace these 0 with the character '-' ?
Just use UPDATE to do this:
UPDATE yourtable
SET col1 = CASE WHEN col1 = '0' THEN '-' ELSE col1 END,
col2 = CASE WHEN col2 = '0' THEN '-' ELSE col2 END,
col3 = ....
In SQL Server:
SELECT REPLACE(Column1,'0','-'),REPLACE(Column2,'0','-'), REPLACE(Column3,'0','-')etc..
FROM YOUR_TABLE_NAME;

SQL Syntax help

What would be the SQL syntax to select 4 different columns in a single row in a table, add those together and then insert that value into a 5th different column in the same row? The columns are all numeric(11,2).
For example- Table name is DataCheck
there is an ID that is primary key so how do I select col1, col2, col3, col4 where ID = 232...etc and add them up, and insert into col4 where id = 232...etc
Unless I'm misunderstanding:
UPDATE MyTable SET col5 = col1 + col2 + col3 + col4 WHERE id = 232
Errr....it doesn't get much simpler than the obvious:
update myTable
set column5 = column1
+ column2
+ column3
+ column4
+ column5
where <some-where-clause>
Why are you storing the calculated value in the same row?
Generally you shold not store the same data twice (in columns 1,2,3,4 and column 5). If somehow they are not equal, how will you know which column is correct?
Complete guess - but does this work?
UPDATE DataCheck SET col5=(col1+col2+col3+col4)
This update statement should solve your problem.
update table set col5 = col4 + col3 + col2 + col1

Computed column should result to string

Here is a snap of my database.
Both col1 and col2 are declared as int.
My ComputedColumn currently adds the Columns 1 and 2, as follows...
col1 col2 ComputedColumn
1 2 3
4 1 5
Instead of this, my ComputedColumn should join the columns 1 and 2 (includimg the '-' character in the middle) as follows...
col1 col2 ComputedColumn
1 2 1-2
4 1 4-1
So, what is the correct syntax?
You're probably defining your computed column as col1+col2. Try CAST(col1 AS NVARCHAR(MAX))+'-'+CAST(col2 AS NVARCHAR(MAX)) instead.
Or if you prefer, you can replace NVARCHAR(MAX) with NVARCHAR(10) or a different length of your choice.
create table TableName
(
col1 int,
col2 int,
ComputedColumn as Convert(varchar, col1) + '-' + Convert(varchar, col2)
)
Bear in mind that if either value is null then the result of ComputedColumn will also be null (using the default collation and settings)
simple:
SELECT ComputedColumn = convert(varchar, col1) + '-' + convert(varchar, col2)
FROM Table
SELECT col1, col2, (col1 + '-' + col2) as ComputedColumn
"+" is both addition and the concatenation character. You could explicitly convert, but in this case, including the '-' in the middle should cause an implicit conversion.
first create table in design mode
add 2 column as col1 and col2
add another column computedcolumn and set computed column property
Also you can use that following script
CREATE TABLE [dbo].[tbl](
[col1] [varchar](50) NOT NULL,
[col2] [varchar](50) NOT NULL,
[ComputedColumn] AS ((CONVERT([varchar],[col1],(0))+'-')+CONVERT([varchar],[col2],(0)))
)