SQL Syntax help - sql

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

Related

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

Combine columns from three different tables into a single column

I am new to SQL and I am not sure what to Google. I have three tables with different numbers of columns. I would like to combine these following three tables into a single column(no duplicates).
Table1
Col1 Col2 Col3
1 a aa
2 b ab
3 c bb
Table2
Col1 Col2
123 Test
456 Test2
346 Test3
Table3
Col1 Col2 Col3 Col4
5695 93234 ABC CDE
4534 92349 MSF KSK
3244 12323 SLE SNE
Expected Output:
FileOutput
1aaa
123Test
569593234ABCCDE
2bab
456Test2
453492349MSFKSK
...
Any help would be much appreciated. Thanks!
The term you would want to Google would be: UNION and CONCAT.
Note: CONCAT is not supported in prior versions to SQL Server 2012.
To get your expected output, I would do this:
select
concat(cast(col1 as varchar(10)),col2,col3) as FileOutput
from table1
UNION
select
concat(cast(col1 as varchar(10)),col2) as FileOutput
from table2
UNION
select
concat(cast(col1 as varchar(10)),cast(col2 as varchar(10)),col3,col4) as FileOutput
from table3
SQL Fiddle Demo
Not sure how you would parse the data, but you could do this:
select convert(varchar(100), col1) + convert(varchar(100), col2) + convert(varchar(100), col3) as fileOutput
from table1
union all
select convert(varchar(100), col1) + convert(varchar(100), col2) as fileOutput
from table2
union all
select convert(varchar(100), col1) + convert(varchar(100), col2) +
convert(varchar(100), col3) + convert(varchar(100), col4) as fileOutput
from table4
note not knowing your column data types, your varchar(100) may need to expand, or could potentially shrink depending on your data.
You can combine them using + (may need to cast your ints as varchars for this to work), then put them all in one table using union all. Example:
Select cast(col1 as varchar(100)) + col2 + col3
from Table1
union all
select cast(col1 as varchar(100)) + col2
from Table2
etc.
Note: be sure to use union all rather than union if you want to keep any duplicates you may create.

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

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