Insert formatted date function using Oracle SQL*PLus? - sql

Am a Oracle PL/SQL newbie. Basically, I have a table created as follows:
CREATE TABLE MYTABLE (
ID VARCHAR2(3 CHAR) NOT NULL PRIMARY KEY,
LAST_UPDATE DATE
);
Am trying to write a script which will run through SQL*Plus command:
insert into MYTABLE (
id,
last_update)
values (
sys_guid,
--- Date call - what is placed here?
);
What can I do to insure that the date inserted is the correct date (at time of insertion) with this format (what function to call):
27-Oct-11
Will need this script to be executable from within SQL*Plus (read that there's numerous amounts of incompatibilities between ANSI SQL & PL/SQL running from SQL Developer vs. SQL*Plus).
Thanks for taking the time to read this!

Date fields don't have a format. You apply a format when you select them by using the to_char function. So what you want to do is
insert into mytable
(id, last_update)
values
(sys_guid, SYSDATE);
And you select from it using
select id, to_char(last_update, 'DD-Mon-YY')
from mytable;

Related

Dates inserting incorrectly - SQL

Dates are not inserting correctly to table, any explanation / solution?
create table test
(
ID bigint,
MarketOpen datetime
);
insert into test (ID, MarketOpen)
values (1, 2019-01-19-11-40-00);
select * from test;
Fiddle
Thats totally the wrong way to enter a date. SQL Server is treating your current syntax as a calculation e.g. 2019-01-19-11-40-00=1948 and then converting the number 1948 to a datetime. You need to use a formatted string e.g.
insert into #test (ID, EventId, MarketId, RaceDate, MarketOpen, HorseID)
values
(1, 123, 153722767, '2019-01-19 11:40:00', '2019-01-18 11:40:00', 34434);
Note: As mentioned by seanb its best practice to use a non-ambiguous format when specifying dates and the ISO format (yyyymmdd) is probably the best of these.

How to insert data in a 'DATE' type sql [duplicate]

This question already has answers here:
Date casting in oracle
(1 answer)
SQL oracle beginner questions
(3 answers)
SQL Date Format Conversion
(3 answers)
Comparing Dates in Oracle SQL
(5 answers)
Closed 4 years ago.
I have created tables like this,
create table customer
(
customer_id number(3),
customer_name varchar(20),
phone varchar(15)
);
create table manager
(
customer_id number(3),
ordered_products number(4),
transaction_on DATE
);
Now how will i insert data in manager table? How to insert into the DATE type?
The answer to this really depends on the type of sql you are using.
In general if the date time is present in the ISO format (yyyymmdd), any sql engine should not face any issues in parsing it. A custom date format would be tricky and specific to individual SQL clients, but here are the broad types to help you get started:
Oracle/PostgreSQL
INSERT INTO manager
(
transaction_on
)
SELECT TO_DATE('23-01-2018', 'dd-mm-yyyy')
FROM DUAL;
This will allow you to parse any date format, as long as you can specify the date format in the following string. The values I have provided are only examples, you can play around with them as per your specific use case. Oracle Reference, Postgres Reference
SQL Server
INSERT INTO manager
(
transaction_on
)
SELECT CONVERT(DATE, '02-25-2018', 101);
Here 101 is style indicator from a pre-specified list, which supports a wide range of date formats. You can select the style indicator based on your specific input string format
MySQL
INSERT INTO manager
(
transaction_on
)
SELECT STR_TO_DATE('May 1, 2013','%M %d,%Y');
This is pretty similar to usage in Postgres and Oracle, except that the date format is represented slightly differently. My SQL Reference
You can insert via SQL Statement:
INSERT INTO manager (customer_id, ordered_products, transaction_on)
VALUES (123, 2, '2018-03-07 05:52:30');
We can provide more accurate answer if we know the database type. (etc. MySQL, MSSQL, PosgreSQL, ...)
As a generic answer: This would probably work:
INSERT INTO manager (
customer_id,
ordered_products,
transaction_on)
VALUES (
001,
5,
'2000-02-28'
);
In MySQL, you can do it in two ways if you want to insert the current date
INSERT INTO manager (customer_id, ordered_products, transaction_on)
VALUES (NULL, '110', NOW());
or for any date you can use
INSERT INTO manager (customer_id, ordered_products, transaction_on) VALUES (NULL,'10', '2018-03-07');
In SQL Server if you want the date to be inserted automatically when the transaction occurs.
ALTER TABLE manager ADD CONSTRAINT DF_DefaultTranDate DEFAULT GETDATE() FOR transaction_on
Whenever a row is inserted in the manager table the transaction_on column will have the server's date and time.

Hadoop - Formatting dates when creating tables

How to format dates during the process of creating Hive tables?
I've currently been dumping some data into a discovery environment at work and storing dates as string, because if I format them as a DATE or TIMESTAMP the values are null.
Here's what the raw data looks like:
12/07/2016 05:07:28 PM
My understanding is that Hive accepts dates in this format
yyyy-mm-dd hh:mm:ss
I can format these using a select statement:
select id, receipt_dt, from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd') as app_dt from MySchema.MyTable where app_num='123456'
How can I add in the statement
from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd')
How can I add this in to the generic CREATE EXTERNAL STATEMENT below so that I no longer have to store dates as a string, or use an ALTER TABLE statement to change the formatting?
CREATE EXTERNAL TABLE IF NOT EXISTS MySchema.My_New_Table
( Field1 Format,
Field2 Format,
Field 3 Format,
)
.......
Use MyTable as staging table with raw data and create final/target table my_new_table with transformations i.e, date format...it will be EDW kind of process...
example:
CREATE EXTERNAL TABLE IF NOT EXISTS MySchema.My_New_Table
( Field1 int,
Field2 string,
Field3 date
)
... more definitions....
AS
select id, receipt_dt,
cast(from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd') as date) as app_dt
from MySchema.MyTable ;
NOTE: This is not tested statement. You may need to try and edit and try...but you got the idea...
Then inserting delta should be similar process...
INSERT INTO TABLE MySchema.My_New_Table
AS
select id, receipt_dt,
cast(from_unixtime(unix_timestamp(receipt_dt ,'MM/dd/yyyy'), 'yyyy-MM-dd') as date) as app_dt
from MySchema.MyTable where <<conditions>>;

How to insert multiple rows in the same table-Oracle 10g

I created a table in Oracle SQL :
create table t1
(
empno number(6) PRIMARY KEY,
empname varchar(30),
hiredate date,
basic number(8),
deptno number(4)
);
And now I am inserting values into the table using a single query:
insert into t1 values((131309,'HP','20-FEB-04',2000000,1235)
(131310,'HT','20-APR-14',120020,1234));
But this shows error:
insert into t1 values((131309,'HP','20-FEB-04',2000000,1235),
*
ERROR at line 1:
ORA-00907: missing right parenthesis
How do I correct this?
An INSERT VALUES statement always inserts exactly 1 row. If you want to insert multiple rows with hard-coded values, the most common approach would simply be to execute two separate INSERT statements.
insert into t1 values(131309,'HP','20-FEB-04',2000000,1235);
insert into t1 values(131310,'HT','20-APR-14',120020,1234);
If you really wanted to, you could select your hard-coded values from dual and then do an INSERT SELECT
insert into t1
select 131309, 'HP', '20-FEB-04',2000000,1235 from dual
union all
select 131310,'HT','20-APR-14',120020,1234 from dual
Or you could do an INSERT ALL
insert all
into t1 values(131309,'HP','20-FEB-04',2000000,1235)
into t1 values(131310,'HT','20-APR-14',120020,1234)
select * from dual
Personally, I'd just use two statements.
Although this isn't related to your question, a couple of comments
Always, always list out the columns in your insert statement. You'll make your SQL much more robust so that if you add new columns in the future that allow NULL values your statements will still work. And you'll avoid lots of bugs when the column list is right there rather than hoping that someone remembers the order of columns in the table.
If you're inserting a value into a date column, use a date not a string literal that represents a date. Relying on implicit data type conversion is a source of many bugs. Use an explicit to_date or use ANSI date literals. And use 4-digit years.

INSERT INTO SELECT FROM ACCESS TO ORACLE

I have a table in Access named TEST_DATE1 with the column TEST_DATE that is a String datatype and the records look like 20080130. yyyymmdd
I have a table in Oracle named TEST with the column TEST_DATE that is a DATE datatype and I want the records to look like 2008/01/30 yyyy/mm/dd.
I have the two tables linked and when I usually update tables between Access and Oracle I usually do a
INSERT INTO TEST
SELECT *
FROM TEST_DATE1;
How would you convert the string to a DATE using the INSERT INTO SELECT
I have tried
INSERT INTO TEST
(SELECT TO_DATE(TEST_DATE, 'yyyy/mm/dd'))
FROM TEST_DATE1;
Thanks!
To move the records to Oracle, converting a string to a date:
INSERT INTO test (test_date)
SELECT TO_DATE(test_date, 'YYYYMMDD')
FROM test_date1
Once it's in Oracle stored as a date, you can retrieve it in any format you like:
SELECT TO_CHAR(test_date, 'YYYY/MM/DD') as test_date
FROM test;