I have a table which have an integer coloumn. Let table name is Table1 and ColName is Col1.
I want to add number 15 to every value in col1. what will be the sql query for this.
If you're just trying to do that via a select:
select col1 + 15 from Table1
or if you need to update the actual rows in the table:
update Table1 set col1 = col1 + 15
This query updates the Col1 values in your table:
UPDATE TABLE Table1
SET Col1=Col1+15
Be carefull - if your column name contains numbers you should use this syntax:
UPDATE TABLE [Table1]
SET [Col1]=[Col1]+15
UPDATE Table1 SET Col1 = (Col1 + 15)
Related
I have a table where each cell in columns has an array list.
COL1 COL2
row1: ('hi','hello') ('hi','hello')
row2: ('hihi','below') ('pi','by')
I am trying to use an in operator on such data but my query is not returning anything.
Query: select * from table where col1 in ('hi')
Also, if I have another table (table2) that looks like this:
col3
____
'hi'
'bye'
'guy'
and I want to use the same concept where I'll be checking if 'hi','bye','guy' from col3 exists in col1 row1
My Query: select * from table2 where col3 in (select col1 from table1);
The first argument is the "inside" one, the second one is the group of values.
You are comparing col1 (a tuple of 2 values) to a unique value:
I think that could help you: col1[0] -> gets the first value
select * from table where col1[0] in ('hi')
Whether you want to compare both, you can use:
select * from table where col1[0] in ('hi') or col1[1] in ('hi')
How do I subtract one table column value from another table column value, and save the value in the first table column in the process?
Thanks for your help.
Where tablename is the table and col1 is the first column and col2 is the second column.
UPDATE tablename SET col1 = (col2 - col1)
If you are meaning that the values are coming from different tables then the query is much the same:
UPDATE tablename SET col1 = ((SELECT col2 from tablename2) - col1)
It's hard to be sure without details about your tables or how they're related, but you might want something like
UPDATE t1
SET col = t1.col - t2.col
FROM mytable t1
JOIN myothertable t2 ON t2.pkCol = t1.pkCol
Not sure what you are looking for, but pick your example:
update CarBrand
set SortIndex = (select SortIndex from CarBrand where CarBrandId = 3615) + 7
where CarBrandId = 3619
update CarBrand
set SortIndex = SortIndex - OtherColumn
where ...
i have two tables TABLE 1 with columns
ID Text email ID
============================
1 This is Test 123#g.com
2 Make my day 1234#g.com
TABLE 2 with one column
words(column)
=============
Test
trip
day
now wat i want to do is compare the text ( each and every word) from Table 1 with each row ow word in TABLE 2, if found then the id should be logged on a different table.
example: if from TABLE1 Test is the word which is the row value in the TABLE 2 word column. so it should log ID =1 in a different table.
also once the word is found it shouldn't go for further iteration.
try this:
select *
from TABLE1
join
TABLE2
on ' '+Text+' ' like '% '+words+' %'
SQL fiddle demo
This works
SELECT t1.*,t2.words
FROM Table1 t1
JOIN Table2 t2
ON PATINDEX('%' + t2.words + '%',t1.text)>0
Output
ID Text email_ID words
1 This is Test 123#g.com Test
2 Make my day 1234#g.com day
You can to use CHARINDEX function:
select *
from TABLE1 t1
where exists
(
select 1
from TABLE2 t2
where CHARINDEX(t2.words,t1.text)>0
)
Link to documentation.
I am trying to find way to do below in SQL table, where a is bit data type
if update col a = 0 all the other values for col a in the table should be updated to 0 and
if update col a = 1 all the other values for col a in the table should be updated to 1.
You can use the INSTEAD OF trigger. It's a Highlander-type trigger, meaning there can only be one of it on a table (for every operation).
CREATE TRIGGER MyTable_SetBitColumn ON MyTable INSTEAD OF UPDATE
AS BEGIN
UPDATE MyTable
SET BitColumn = i.BitColumn
FROM (SELECT TOP 1 * FROM inserted) i
END
Do you mean you want to toggle all values for that column? If so:
UPDATE table
SET BitColumn = 1 - BitColumn
How about
UPDATE mytable SET a=1
or
UPDATE mytable SET a=0
If all the rows in the table will have the same value then don't store this column in this table. Store it in another table with 1 row. You can then cross join onto the single row table when doing your queries.
I have a table with some duplicate rows. I want to modify only the duplicate rows as follows.
Before:
id col1
------------
1 vvvv
2 vvvv
3 vvvv
After:
id col1
------------
1 vvvv
2 vvvv-2
3 vvvv-3
Col1 is appended with a hyphen and the value of id column.
This SQL will only update duplicates, but not the one with the lowest id :
update tbl
set col1 = col1 + '-' + convert(varchar, id)
where exists(select * from tbl t where t.col1 = tbl.col1 and t.id < tbl.id)
Check IN out in Oracle Syntax. The query is not tested
update table1 set
col1 = col1 || id
where
id not in (
select min(id) from table1
groupby col1
)
You might be able to do that with a sproc and cursors. I don't think it's possible in any reasonable select query.
You can accomplish this with a 2 step process, although an SQL wizard could probably modify this to give you a solution in one step.
First you need to get all the duplicate values. Here is an SQL query that will do that:
SELECT COUNT(*) AS NumberOfDuplicates, col1
FROM Table1
GROUP BY col1
HAVING (COUNT(*) > 1)
This will give you a resultset listing the number of duplicates and the duplicate value.
In step 2 you would loop through this resultset, fetch the col1 value, return all the records containing that value and (possibly using a loop counter variable) alter the value as per your example.
Note: you don't really need to return the number of duplicates to achieve your goal, but it will help you to test the query and be satisfied that it works.