SQL INSERT with sub query - sql

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.

Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId

Related

Truncate not workng in MySQL

I guess the correct way of truncating a value in MySQL is truncate(value,limit); but that doesn't seem to be working here it needs an extra table name;
select truncate(94204.27348,2);
ERROR at line1:
ORA-00923: FROM keyword not found where expected
Assuming you are using Oracle, you must supply a table reference on your select query.
Oracle select queries require a table reference always.
You can do this....
Select truncate(94204.27348,2) -- see further comment below
From dual
;
Dual is a special table that allows these sort of queries.
Also, I think you might mean to use the TRUNC function.
As others have pointed out, the TRUNCATE query is not quite the same, it will erase the contents of the table that you supply it.
In Mysql You should use round. Truncating table will remove the data from the table and reset all your auto increment values.
SELECT ROUND(94204.27348,2);
Result - 94204.27
This will round the value and display only two decimal places.

Oracle SQL command not properly ended

I am doing a simple insert and am stumped, I'm new to oracle and unsure of what the issue is. I don't have the table structure so I am guessing that most of the fields are character except the dates.
Anyway here is my query, can anyone find the issue?
INSERT INTO PHANTOM_BOXES (CARRIER_CODE,CARRIER_TRACKING_NO,SENT_DATE,SEND_COST,
RECEIVED_DATE,REC_COST, COMMENTS,SHIPPING_TECH,RECEIVING_TECH)
VALUES ('1','11',TO_DATE('2016-02-04','YYYY-MM-DD'),'1',
TO_DATE('2016-02-04','YYYY-MM-DD'),'1','1','26437','0')
WHERE BOX_NO = '6738'
NO where dude.what is ther where for.
INSERT INTO PHANTOM_BOXES (CARRIER_CODE,CARRIER_TRACKING_NO,SENT_DATE,SEND_COST,
RECEIVED_DATE,REC_COST, COMMENTS,SHIPPING_TECH,RECEIVING_TECH,BOX_NO)
VALUES ('1','11',TO_DATE('2016-02-04','YYYY-MM-DD'),'1',
TO_DATE('2016-02-04','YYYY-MM-DD'),'1','1','26437','0','6738')
WHERE BOX_NO = '6738'
INSERT statement cannot have a WHERE clause, makes no sense.
Simply do INSERT INTO..VALUES:
INSERT INTO PHANTOM_BOXES (CARRIER_CODE,CARRIER_TRACKING_NO,SENT_DATE,SEND_COST,
RECEIVED_DATE,REC_COST, COMMENTS,SHIPPING_TECH,RECEIVING_TECH)
VALUES ('1','11',TO_DATE('2016-02-04','YYYY-MM-DD'),'1',
TO_DATE('2016-02-04','YYYY-MM-DD'),'1','1','26437','0')
Where Clause is used for filter and applying condition Rows which were already present in Table.
Seems you are trying to update the values for WHERE BOX_NO = '6738'
For this you have to use Update Statement
Update PHANTOM_BOXES
Set CARRIER_CODE='1',
CARRIER_TRACKING_NO='11',
SENT_DATE=TO_DATE('2016-02-04','YYYY-MM-DD'),
SEND_COST='1',
RECEIVED_DATE=TO_DATE('2016-02-04','YYYY-MM-DD'),
REC_COST='1',
COMMENTS,SHIPPING_TECH='26437',
RECEIVING_TECH='0';

Moving everything from one table to another in sql server while converting one of the rows

I am trying to move everything from one table to another table. I know how to do that if all of the columns had the same data type, however one column is different. On the new table its varcahar(24) and on the old table its a bigint.
This is what i have so far, Im using the first select statement to set the id, and the rest to add it to the new table. However this always returns the last id, and i need it to be synced up with the second select statement
Any suggestions on how to do this would be awesome. i tried to google it for about an hour but couldnt come up with anything. I am using SQL Server 2012
use DatabaseA
GO
DECLARE #id bigint;
SELECT #id= columnName FROM differentDB.tableB
INSERT INTO tableA
(buyer_id, restOfTheColumns)
Select CAST(#id AS varchar(24)), restOfTheColumns
FROM differentDB.tableB
GO
Variables don't work like that. T-SQL variables hold data values, not object names. Furthermore, that's a scalar variable. It only holds one value.
That said, you don't need the variable at all. You can just do this:
INSERT INTO tableA (buyer_id, restOfTheColumns)
SELECT CAST(columnName AS varchar(24)), restOfTheColumns
FROM differentDB.tableB

Inserting a new column into SQL

I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory

SQL LIKE query not working

I'm trying to run the following query against an Oracle DB, but the query is returning 0 records:
select * from TABLE
where upper(FIELD) like '%SEE COMMENT%'
I know this field contains many records with 'See Comment" in it. For example, here is one of the records:
=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))
I am guessing that the quotation marks in the field are messing the query up, but im not sure how to get around it. Any suggestions?
This works for me:
create table testLike (aCol varchar2(500) );
INSERT INTO TESTLIKE VALUES('abc');
insert into testLike values('=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))');
SELECT *
FROM TESTLIKE TL
WHERE upper(tl.acol) like '%SEE COMMENT%';
can you recreate?
edit:
in your query try this:
select * from TABLE
WHERE UPPER(FIELD) = '=if(and(Robust_Mean>=20,Robust_Mean<=70),.03*(Robust_Mean+29),
if(Robust_Mean>70,.083*(Robust_Mean^.9),"See Comment"))';
see if that comes up with any results
Just realized there were two similarly named fields in this table, and I was choosing the wrong one.