Computed column should result to string - sql

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)))
)

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 Server : Reuse calculated variable in select clause

I have the following table structure:
col1 col2 col3 col4
-------------------------------
aK Mbcd ABc defgh
col2, col3 and col4 columns are of type varchar(100) and col1 has type varchar(500).
I need a select query to have the output as following
col1 col2 col3 col4
-------------------------------
aK,Mb cd,A Bc,d efgh
Logic is explained as mentioned below:
In the result, Col2, col3 and col4 can have maximum 4 characters but col1 can have more than 4 characters upto 100.
If any column has more characters, last 4 characters will be retained in the same column and other extra columns will be concatenated with previous column's value separated by comma , and the same rule will be applied on the concatenated values as well.
I've written the following T-SQL statement. It works fine for last two columns. But I want to use new calculated value of col3 to strip out extra characters after adding some from col4
SELECT
CASE
WHEN X.Col4Length > 4
THEN concat(X.col3, ',', substring(x.col4, 0, X.Col4Length - 3))
ELSE X.col3
END AS col3,
CASE
WHEN X.Col4Length > 4
THEN substring(x.col4, X.Col4Length - 3, x.Col4Length)
ELSE X.col4
END AS col4
FROM
(SELECT
Col1, Col2, Col3, Col4,
Len(Col1) AS Col1Length,
Len(Col2) AS Col2Length,
Len(Col3) AS Col3Length,
Len(Col4) AS Col4Length
FROM
mytable) X
My try with a simple sub-query
with t1 as (
select 'aK' col1, 'Mbcd' col2, 'ABc' col3, 'defgh' col4
---
SELECT LEFT(col, LEN(col) - 12) col1,
RIGHT(LEFT(col, LEN(col) - 8), 4) col2,
RIGHT(LEFT(col, LEN(col) - 4), 4) col3,
RIGHT(col, 4) AS col4
FROM
(
SELECT col1+','+col2+','+col3+','+col4 AS col
FROM t1
) t;
You want to reuse calculated variables
There are two set-based /inline / adhoc approaches (and many more ugly procedural):
CTEs to do this for the whole set in advance
CROSS APPLY for the same on row level
Try it like this (CTE approach)
DECLARE #tbl TABLE(col1 VARCHAR(100),col2 VARCHAR(100),col3 VARCHAR(100),col4 VARCHAR(100));
INSERT INTO #tbl VALUES
('aK','Mbcd','ABc','defgh')
,('123456','abc','3456','123456789');
WITH ResolveCol4 AS
(
SELECT *
,RIGHT(col4,4) AS Col4_resolved
,col3 + ',' + CASE WHEN LEN(col4)>4 THEN SUBSTRING(col4,1,LEN(col4)-4) ELSE '' END AS col3_New
FROM #tbl
)
,ResolveCol3 AS
(
SELECT *
,RIGHT(col3_New,4) AS Col3_resolved
,col2 + ',' + CASE WHEN LEN(col3_New)>4 THEN SUBSTRING(col3_New,1,LEN(col3_New)-4) ELSE '' END AS col2_New
FROM ResolveCol4
)
,ResolveCol2 AS
(
SELECT *
,RIGHT(col2_New,4) AS Col2_resolved
,col1 + ',' + CASE WHEN LEN(col2_New)>4 THEN SUBSTRING(col2_New,1,LEN(col2_New)-4) ELSE '' END AS col1_New
FROM ResolveCol3
)
SELECT col1_new,Col2_resolved,Col3_resolved,Col4_resolved
FROM ResolveCol2
The result
aK,Mb cd,A Bc,d efgh
123456,abc,34 56,1 2345 6789

Concat in update

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])

SQL Server: select value, split on a delimiter, then update two columns

I have a SQL Server 2008 database table with three varchar Columns - Col1, Col2, Col3. Col1 has data in it with a single space in between, Col2 and Col3 are empty.
I need to write a query to select the data from Col1, break up each value using the space as the delimiter, and inserting the data on either side of the space into Col2 and Col3 respectively.
I am not too sure how to proceed. Can this be accomplished in SQL, or should I create a small program to do the work for me? I'd appreciate a pointer in the right direction if this can be accomplished via SQL.
Thanks.
UPDATE table SET
Col2 = SUBSTRING(Col1, 1, CHARINDEX(' ', Col1)-1),
Col3 = SUBSTRING(Col1, CHARINDEX(' ', Col1)+1, 8000)
WHERE Col1 LIKE '% %';
If you can guarantee there is only one space:
create table #temp (col1 varchar(50),col2 varchar(50), col3 varchar(50))
insert into #temp (col1)
select 'test 1'
union all
select 'test 2'
union all
select 'test 3'
update #temp
set col2 = left(col1, charindex (' ', col1)),
col3 = substring(col1,charindex (' ', col1)+1, len(col1))
from #temp

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