I am currently trying to update a column in a temporary table using Oracle 11g SQL syntax. In this column there is an Unique ID that is 12 digits long. However I need to join this table with this column holding the Unique ID but the syntax for the Unique ID of this table is slightly different than the syntax for the table that it will be joined (with Unique ID serving as the PK = FK). This may be tough to follow so I will provide what I am doing below.
UniqueID Column from TABLE xyz Syntax
AB10783421111111
UniqueID Column from TABLE zxo Syntax
383421111111
You see how the numbers are identical except for the AB107 and first '3' in the zxo table? I would like to know why both these queries are not running
UPDATE temp37 SET UNIQUE_ID = REPLACE(UNIQUE_ID, (LIKE 'AB107%'), (LIKE '3%'));
UPDATE temp37
SET UNIQUE_ID = '3%'
WHERE UNIQUE_ID = 'AB107%';
Essentially I would like to replace every case of an id with AB10755555555555 to 355555555555. Thank you for any help.
You can do:
UPDATE temp37 SET UNIQUE_ID = REPLACE(UNIQUE_ID, 'AB107', '3');
OR
UPDATE temp37 SET UNIQUE_ID = CONCAT('3', substr(UNIQUE_ID, 6)) WHERE UNIQUE_ID LIKE 'AB107%';
Related
This might be simple, but I am having troubles resolving this issue
I have a table with following columns and data :
There are multiple entries for an id with different updateTimestamp, I want to identify the max timestamp and repeat that value for all the duplicate ids in the table. Also, the table is large and I do not want to query it multiple times(process is complex).
Here is what I am expecting the output to look like
You need to find maximum of update timestamp for each ID and update the table using ID column. Something like
update <table_name> tgt
set tgt.max_updatetimestamp = src.maxstamp
from (select id, max(updatetimestamp) as maxstamp from <tablename> group by 1) src
where tgt.id = src.id;
I have the following update SQL that works for a single row.
UPDATE myshcema.my_table
SET emp_id = '000987654321'
WHERE cust_id = '000123456789';
But I have over 6500 unique cust_ids that = 6500 unique emp_id. I would like to update all of my emp_id column with the corresponding cust_id in the cust_id coulmn. This table has other information in it but I'm missing about 5000 emp_id. I have a .csv that has the cust_id, emp_id.
Any help would be appreciated.
From the basic information you've provided, it sounds like you need to use a from statement in your update. First you need to load that CSV into a table on your DB.
This statement can be different based on what db you're using, but you should get the gist of it
UPDATE my_table tbl
SET emp_id = csv.emp_id
FROM your_new_csv_table csv
WHERE tbl.cust_id = csv.cust_id;
here is my situation,
I have 2 tables,
1st table has all records, and it has IDs
2nd table has new records and it doesnt have ID, yet.
I want to generate ID for 2nd table with max(id) + 1 from 1st table.
when i do this, it makes all rows same id number, but i want to make it unique increment number.
e.g
select max(id) from table1 then it gives '997040'
I want to make second table rows like;
id
997041
997042
997043
997044
i think i need to use cursor or whileloop, or both, but i could not create the actual query.
sorry about bad explanation, i am so confused now
Use ROWNUM to generate incrementing row numbers. E.g.:
SELECT someConstant + ROWNUM FROM source.
CREATE TABLE table_name
(
ID int IDENTITY(997041,1) PRIMARY KEY
)
I hope this sql query would work!!
Or refer http://www.w3schools.com/sql/sql_autoincrement.asp
I want to merge the IndirectFlights table to the PriceTable.
I do not have IDs entered in the SourceTable (IndirectFlights) and I haven't set a PK for it yet.
The ID column for the PriceTable is an Identity (1,1) column and is also the Primary Key.
Qs1 How do I enter IDs in Source column so that they dont clash with target table (PriceTable) IDs? I was thinking of using a sequence but It potentially could clash in future.
Qs2 Can I choose what columns to merge or must I merge all the columns from the Source table?
Target Table (PriceTable) Columns
IDAirport_ICAO_Code,Airline_ICAO_Code,Departure,Price,RouteStatus,DateRowModified
Source Table (IndirectFlights) Columns
IDAirport_ICAO_Code,Destination,Airline,Airline_ICAO_Code,RouteStatus,Connecting Airport
Edit: I have just run the following Union All statement as an alternative to using Merge.
Select ID,Airport_ICAO_Code,Airline_ICAO_Code,RouteStatus
From RoughworkPriceTable
Union All
Select ID,Airport_ICAO_Code,Airline_ICAO_Code,RouteStatus
From RoughworkIndirectFlights;
The code worked but i noticed that the ID column accepted the Null values from IndirectFlights.ID eventhough I have the ID columns set to Not Null.
Can anyone explain this.
Also can someone expalin how I could create a new permanent table from this Union All statement.
You can create a new table with something like
Select * into newTmpTable from (
Select ID,Airport_ICAO_Code,Airline_ICAO_Code,RouteStatus From RoughworkPriceTable
Union All
Select ID,Airport_ICAO_Code,Airline_ICAO_Code,RouteStatus From RoughworkIndirectFlights)
as mergedData;
I have this table:
Table1:
id text
1 lala
And i want take first row and copy it, but the id 1 change to 2.
Can you help me with this problem?
A SQL table has no concept of "first" row. You can however select a row based on its characteristics. So, the following would work:
insert into Table1(id, text)
select 2, text
from Table1
where id = 1;
As another note, when creating the table, you can have the id column be auto-incremented. The syntax varies from database to database. If id were auto-incremented, then you could just do:
insert into Table1(text)
select text
from Table1
where id = 1;
And you would be confident that the new row would have a unique id.
Kate - Gordon's answer is technically correct. However, I would like to know more about why you want to do this.
If you're intent is to have the field increment with the insertion of each new row, manually setting the id column value isn't a great idea - it becomes very easy for there to be a conflict with two rows attempting to use the same id at the same time.
I would recommend using an IDENTITY field for this (MS SQL Server -- use an AUTO_INCREMENT field in MySQL). You could then do the insert as follows:
INSERT INTO Table1 (text)
SELECT text
FROM Table1
WHERE id = 1
SQL Server would automatically assign a new, unique value to the id field.