SQL -Find and Replace the value in the table - sql

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;

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

Average without avg function

I need to calculate average value from 6 columns but when there is an empty field I don't want to have it calculated.
For example, if I have (2, 3, 2, 3, 2, 3) I should get 15/6 = 2,5 but when it is (2, 3, 2, 3, 'empty', 'empty') I need to have 10/4 = 2,5.
How to achieve this?
This is average from 6 columns co I can't use avg function which ignores NULLs as default.
Ignoring nulls is QUITE easy, sqlserver already ignores null values:
SELECT avg(x)
FROM (values(4),(6),(null)) x(x)
Result
5
Edit since your comment says your numeric column is a varchar - which is silly, do this instead to handle empty and null values:
SELECT
avg(cast(nullif(x, '') as decimal(18,2)))
FROM (values('4'),('6'),(null), ('')) x(x)
You can create a view or subquery which skips rows with "empty" values and take an average on the view or subquery.
SELECT AVG(value) FROM
(SELECT value FROM table WHERE value <> the_value_that_janek_considers_as_empty)
But I would seriously advise you to reconsider your notion of "empty". There is no such thing as an empty numeric value in databases. Either you say "empty" when in fact you mean "null", or you are doing something horrible, like storing numbers in textual columns.
Catch your NULL values by using CASE statement. See Below:
SELECT
AVG(ISNULL(Column_to_Average,0)[Column_to_Average])
FROM #yourtable
OR
SELECT
AVG(CASE WHEN Column_to_Average IS NULL OR Column_to_Average=''
THEN 0 ELSE Column_to_Average END)
FROM #yourtable
OR You can also do this if you want to get the average of each rows.
SELECT
(
(CASE WHEN Column_to_Average_1 IS NULL OR Column_to_Average_1='' THEN 0 ELSE Column_to_Average_1 END) +
(CASE WHEN Column_to_Average_2 IS NULL OR Column_to_Average_2='' THEN 0 ELSE Column_to_Average_2 END) +
(CASE WHEN Column_to_Average_3 IS NULL OR Column_to_Average_3='' THEN 0 ELSE Column_to_Average_3 END) +
(CASE WHEN Column_to_Average_4 IS NULL OR Column_to_Average_4='' THEN 0 ELSE Column_to_Average_4 END) +
(CASE WHEN Column_to_Average_5 IS NULL OR Column_to_Average_5='' THEN 0 ELSE Column_to_Average_5 END) +
(CASE WHEN Column_to_Average_6 IS NULL OR Column_to_Average_6='' THEN 0 ELSE Column_to_Average_6 END)
)/6 Column_to_Average
FROM #YourTable
select sun(x) --sql server ignore null,
count(x) --sql server ignore null,
sum(x)/count(x) as avgx
from tbl
You face two problems:
You have to convert the strings to numbers, and avoid the null and empty strings.
You need to calculate the average of columns, so the avg function isn't usable as long as the data is in that form.
Basically there are two possible approaches, either you can just use the column values, or you can turn the columns into rows.
Using the column values naturally becomes repetetive. You have to both convert each column, and check how many usable columns there are:
select
(
case when Col1 is null or Col1 = '' then 0.0 else cast(Col1 as float) end +
case when Col2 is null or Col2 = '' then 0.0 else cast(Col2 as float) end +
case when Col3 is null or Col3 = '' then 0.0 else cast(Col3 as float) end +
case when Col4 is null or Col4 = '' then 0.0 else cast(Col4 as float) end +
case when Col5 is null or Col5 = '' then 0.0 else cast(Col5 as float) end +
case when Col6 is null or Col6 = '' then 0.0 else cast(Col6 as float) end
) / (
case when Col1 is null or Col1 = '' then 0.0 else 1.0 end +
case when Col2 is null or Col2 = '' then 0.0 else 1.0 end +
case when Col3 is null or Col3 = '' then 0.0 else 1.0 end +
case when Col4 is null or Col4 = '' then 0.0 else 1.0 end +
case when Col5 is null or Col5 = '' then 0.0 else 1.0 end +
case when Col6 is null or Col6 = '' then 0.0 else 1.0 end
) as Average
from
TheTable
You could create user defined function to do the conversion and counting, but you still need to do it for each column.
(A word of caution also; if there isn't a value in any of the columns, this would give you a division by zero error.)
You can use unpivot to turn the columns into rows, but you need a unique value to group the result on to keep the rows together that belong together. Example:
select
Id, avg(cast(Col as float))
from
TheTable
unpivot
(Col for ColName in (Col1, Col2, Col3, Col4, Col5, Col6)) x
where
Col <> '' and Col is not null
group by
Id

Compare 2 different string columns in an SQL database

I have two different columns of strings and I want to check if one is contained in the other and create a third column which has values of either 1 or 0( 1 if column2 contains column 1 and 0 if otherwise)
For example:
Column1 Column2 Column3
Spar I am Sparta 1
How are you Sparta 0
How do you do this string comparison in SQL ?
You could use the charindex function. Assuming you just want to check if colum1 is contained in column2:
SELECT CASE WHEN CHARINDEX(column1, column2) > 0
THEN 1
ELSE 0
END AS column3
FROM mytable
If you need to check both ways, just add another call:
SELECT CASE WHEN CHARINDEX(column1, column2) > 0 OR
CHARINDEX(column2, column1) > 0
THEN 1
ELSE 0
END AS column3
FROM mytable
Try this:
WITH temp(col1, col2) AS(
SELECT 'Spar', 'I am Sparta' UNION ALL
SELECT 'How are you', 'Sparta'
)
SELECT
*,
col3 =
CASE
WHEN col1 like '%' + col2 + '%' or col2 like '%' + col1 + '%' THEN 1
ELSE 0
END
FROM temp
It should work
select column1,column2, (case when column2 like '%' + column1 + '%' then 1 else 0 end) as column3 FROM yourtable
Try this
DECLARE #table1 TABLE
(
Column1 VARCHAR(20),
Column2 VARCHAR(20)
)
INSERT INTO #table1
VALUES ('Spar',
'I am Sparta'),
('How are you',
'Sparta')
SELECT column1,
column2,
CASE
WHEN CHARINDEX(column1, column2) > 0
OR CHARINDEX(column1, column2) > 0 THEN 1
ELSE 0
END AS column3
FROM #table1

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