SQL>
SQL> insert into employees values('&id','&fname','&lname','&numbermo','&yyy','&jobid','&MonthlyS','&managerID','&Did');
Enter value for id: 10
Enter value for fname: ewssfws
Enter value for lname: weffs
Enter value for numbermo: 987654321
Enter value for yyy: To_Date('2020/10/10')
Enter value for jobid: J1
Enter value for monthlys: 25000
Enter value for managerid: 20
Enter value for did: A2
old 1: insert into employees values('&id','&fname','&lname','&numbermo','&yyy','&jobid','&MonthlyS','&managerID','&Did')
new 1: insert into employees values('10','ewssfws','weffs','987654321','To_Date('2020/10/10')','J1','25000','20','A2')
insert into employees values('10','ewssfws','weffs','987654321','To_Date('2020/10/10')','J1','25000','20','A2')
*
ERROR at line 1:
ORA-00917: missing comma
The value of y contains single quotes, which breaks the string. You need to escape them by doubling them:
To_Date(''2020/10/10'')
Note that these are two consecutive single quotes characters ('), not the double quotes character (").
As Oracle tells you, error comes for the YYY parameter. You have to use double single quotes.
Though, applying TO_DATE to a string without format mask is bad practice. If I were you, I'd insert a date literal instead, which is always date 'yyyy-mm-dd'. So:
SQL> create table test (id number, yyy date);
Table created.
SQL> insert into test (id, yyy) values (&id, &yyy);
Enter value for id: 1
Enter value for yyy: date '2020-08-28'
old 1: insert into test (id, yyy) values (&id, &yyy)
new 1: insert into test (id, yyy) values (1, date '2020-08-28')
1 row created.
SQL> select * from test;
ID YYY
---------- ----------------
1 28.08.2020 00:00
SQL>
I don't know what issue you might have with your particular Oracle tool, but I wanted to also point out that your current call to TO_DATE won't work, and will generate this error:
ORA-01861: literal does not match format string
Consider this version:
INSERT INTO employees
VALUES
('10', 'ewssfws', 'weffs', '987654321', date '2020-10-10', 'J1', '25000', '20', 'A2');
Related
I have a table which I have cut down into basic fields, it is called Customer
ID | Name | Type
1 | Smith | 2
I want to create a trigger on INSERT that will change the value of the inserting Type, into a number example:
INSERT INTO Customer (Name,Type) VALUES ('Jones', 'Recommended')
The Type field should be a number and it is set as an INT column. I do not want to change this away from INT.
How can I force the word Recommended to be changed to ‘0’ a zero?
In Theory :
This is the trigger :
ALTER TRIGGER [dbo].[CustomersInsert] ON [dbo].[Customer]
INSTEAD OF INSERT
AS BEGIN
INSERT Into Customer (Name,Type)
SELECT
inserted.[Name],
Isnull([Types].Id, 0)
FROM inserted
Left Join [dbo].[Types] on inserted.[Type] = [Types].Caption
END
The [dbo].[Types] is for keep Types.
But in real :
You can't execute INSERT INTO Customer (Name,Type) VALUES ('Jones', 'Recommended')
because Type is INT and 'Recommended' is not.
I'm trying to figure out LPAD function in SQL Server. I found some syntax but the result is not what I'm expecting. The column ID is defined as varchar. I want to replicate dependent string '04130' based on the length of ID column. Any hints is highly appreciated. Thanks
SELECT
ID,
RIGHT(REPLICATE('04130', 12)+LEFT(ID, 12), 12) --- (LPAD in sql server)
FROM
S_Information_Tab;
This is the output I'm getting
ID (No column name)
6452 130041306452
6495 130041306495
This is the expected output:
ID (No column name)
6452 041300416452
6495 041300416495
I think I understand your issue. The length of the replicated string is dependent on the length of your ID. I think it would be best to find the length of the string, subtract that from the final length then concat your replicated string as below:
select concat(substring(REPLICATE('04130', 12),1,12-len(id)),id)
FROM S_Information_Tab;
Assuming your ID is of nvarchar type, this would give you the expected OUTPUT.
SELECT
ID,
CONCAT(LEFT(REPLICATE('04130', 12),8),LEFT(ID,4))
FROM
S_information_tab;
Also check these links:
https://msdn.microsoft.com/en-us/library/ms177601.aspx
https://msdn.microsoft.com/en-us/library/ms174383.aspx
For testing purpose
CREATE TABLE #TMP (
ID NVARCHAR(10)
);
INSERT INTO #TMP (ID)
VALUES
('6452')
,('6495');
SELECT
ID
,CONCAT(LEFT(REPLICATE('04130', 12),8),LEFT(ID,4))
FROM
#TMP;
DROP TABLE #TMP;
The simplest way to achieve the desired result is:
-- Prefixing a string field.
SELECT
'04130041' + ID
FROM
(
-- Embedded sample rows.
VALUES
('6452'),
('6495')
) AS S_Information_Tab(ID)
;
Here I've used a table value constructor to generate the sample values from your question.
Left returns x characters from the start of a string. If the required number exceeds the length of the string, the entire string is returned. Examples:
-- Left examples.
SELECT
LEFT('123', 1) AS [R1],
LEFT('123', 2) AS [R2],
LEFT('123', 3) AS [R3],
LEFT('123', 4) AS [R4]
;
Returns
R1 R2 R3 R4
1 12 123 123
How do I write and execute a query which inserts array values using libpqxx?
INSERT INTO exampleTable(exampleArray[3]) VALUES('{1, 2, 3}');
This example code gives me:
ERROR: syntax error at or near "'"
What is wrong? In PostgreSQL documentation I found that:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
...
INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"training", "presentation"}}');
You should use a column name without an index to insert an array:
create table example(arr smallint[]);
insert into example(arr) values('{1, 2, 3}');
-- alternative syntax
-- insert into example(arr) values(array[1, 2, 3]);
select * from example;
arr
---------
{1,2,3}
(1 row)
Use the column name with an index to access a single element of the array:
select arr[2] as "arr[2]"
from example;
arr[2]
--------
2
(1 row)
update example set arr[2] = 10;
select * from example;
arr
----------
{1,10,3}
(1 row)
You can use arr[n] in INSERT but this has special meaning. With this syntax you can create an array with one element indexed from the given number:
delete from example;
insert into example(arr[3]) values (1);
select * from example;
arr
-----------
[3:3]={1}
(1 row)
As a result you have an array which lower bound is 3:
select arr[3] from example;
arr
-----
1
(1 row)
ref: https://ubiq.co/database-blog/how-to-insert-into-array-in-postgresql/
A. use ARRAY
insert into employees (id, name, phone_numbers)
values (1, ' John Doe', ARRAY ['9998765432','9991234567']);
// less nested quotes
B. use '{}'
insert into employees (id, name, phone_numbers)
values (2, ' Jim Doe', '{"9996587432","9891334567"}');
OR
insert into employees (id, name, phone_numbers)
values (2, ' Jim Doe', '{9996587432,9891334567}');
// seems inner quotes " not necessary,
// number or string depends on column type.
I have a table A with 2 columns with Group and Age. I need to insert the only 3 entries in the age column to a different table B which has columns Age 1, age 2, age 3. How can I do that.
INSERT INTO B (Age1, Age 2, Age 3)
Values/ select .....
TABLE A
#GROUP | AGE
#AGE1 23
#AGE2 25
AGE3 29
TABLE B
#(ID | AGE1 | AGE2 | AGE3)
#(1 -- -- ---)
CATCH: I cannot hard code in this case. is there any way of using cursors in dynamic sql to get this done
Thanks. I figured out the solution. All I needed to do is get both the columns in a COMMA separated varchar variable. Then pass the variables in a dynamic sql in the insert values.
Insert into table B
('+#rowsincolumn1+')
values('+rowsincolumn2+')
Make sure you insert Single quotes in your varchar variable after every value to make it run in the dyn sql.
I am having difficulties subtracting data from the same column. My data structure looks like this:
TimeStamp ID state Gallons
1/01/2012 1 NY 100
1/05/2012 1 NY 90
1/10/2012 1 NY 70
1/15/2012 1 NY 40
So I would like to get the result of (100-40) = 60 gallons used for that time period. I tried using the following query:
SELECT T1.Gallons, T1.Gallons -T1.Gallons AS 'Gallons used'
FROM Table 1 AS T1
where Date = '2012-07-01'
ORDER BY Gallons
It seems like a simple way of doing this. However, I can not find an efficient way of including the timestamp as a way of retrieving the right values (ex. gallon value on 1/1/2012 - gallon value on 1/30/2012).
ps. The value of "Gallons" will always decrease
select max(gallons) - min(gallons)
from table1
group by Date
having Date = '2012-07-01'
try
select max(gallons) - min(gallons)
from table1 t1
where date between '2012-01-01' and '2012-01-31'
SELECT MAX(gallons) - MIN(gallons) AS 'GallonsUsed'
FROM table;
would work in this instance, if you are doing a monthly report this could work. How does the value of gallons never increase? what happens when you run out?
Will you always be comparing the maximum and minimum amount? If not, maybe something like the following will suffice (generic sample)? The first time you'll run the script you'll get an error but you can disregard it.
drop table TableName
create table TableName (id int, value int)
insert into TableName values (3, 20)
insert into TableName values (4, 17)
insert into TableName values (5, 16)
insert into TableName values (6, 12)
insert into TableName values (7, 10)
select t1.value - t2.value
from TableName as t1 inner join TableName as t2
on t1.id = 5 and t2.id = 6
The thing you're asking for is the last line. After we've inner joined the table onto itself, we simply request to match the two lines with certain ids. Or, as in your case, the different dates.