How can i insert DateTime in SQL? - sql

how can i insert this DateTime in SQL?
1/21/2020 8:30:53.527000 AM +00:00
I know that for 12/12/2020 we can make TO_DATE('12/12/2020', 'DD/MM/YYYY')

In Oracle, in your INSERT statement you can use a TIMESTAMP literal:
TIMESTAMP '2020-01-21 08:30:53.527000 +00:00'
Or use TO_TIMESTAMP_TZ:
TO_TIMESTAMP_TZ(
'1/21/2020 8:30:53.527000 AM +00:00',
'MM/DD/YYYY HH12:MI:SS.FF6 AM TZH:TZM'
)

Related

filter timestamp column in SQL Oracle

I have a table with a Timestamp column which I need to filter after '2020-08-26', but every solution won't work
RUN_TIME
2020-07-22 04:22:07
2020-07-22 04:34:07
2020-07-22 04:45:07
2020-07-22 04:50:07
2020-07-22 04:55:08
I tried the below queries:
WHERE CAST (RUN_DATE AS DATE) >= TO_DATE('2020-08-26', 'yyyy-mm-dd')
WHERE to_date(RUN_DATE, 'YYYY-MM-DD hh24:mi:ss') >= to_date('26-08-2020 23:59:59', 'YYYY-MM-DD hh24:mi:ss')
WHERE RUN_DATE >= TO_TIMESTAMP('26-08-2020 23:59:59')
Use a TIMESTAMP literal:
SELECT *
FROM table_name
WHERE run_time >= TIMESTAMP '2020-08-27 00:00:00';
or, use a DATE literal:
SELECT *
FROM table_name
WHERE run_time >= DATE '2020-08-27';
or, use TO_TIMESTAMP with a format model:
SELECT *
FROM table_name
WHERE run_time >= TO_TIMESTAMP( '2020-08-27', 'YYYY-MM-DD' );
or, use TO_DATE with a format model:
SELECT *
FROM table_name
WHERE run_time >= TO_DATE( '2020-08-27', 'YYYY-MM-DD' );
Which for the sample data:
CREATE TABLE TABLE_NAME ( run_time TIMESTAMP(6) );
INSERT INTO table_name ( run_time )
SELECT TIMESTAMP '2020-07-22 04:22:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:34:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:45:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:50:07' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-07-22 04:55:08' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-08-26 23:59:59.999999' FROM DUAL UNION ALL
SELECT TIMESTAMP '2020-08-27 00:00:00' FROM DUAL;
All output:
| RUN_TIME |
| :------------------------ |
| 27-AUG-20 00.00.00.000000 |
db<>fiddle here
Just:
where run_date >= date '2020-08-26'
That's direct filtering against a date literal.
run_date is a timestamp already, so you should not attempt to convert it. Also, to_timestamp() takes a second argument, which specifies the format of the string; without it, it defaults to the nls_timestamp_format of your session (or database), which may not be what you expect

Can I insert date and time with AM/PM to DATE datatype?

INSERT INTO OFFICE VALUES( 23,TO_DATE('07-Jan-2018 12:30 AM', 'DD-MON-YYYY HH24:MI TT'),'Meadow Fair');
Error report -
ORA-01821: date format not recognized
Name Null? Type
------------ -------- -------------
OFF_NO NOT NULL NUMBER(8)
OFF_DATETIME NOT NULL DATE
OFF_LOCATION NOT NULL VARCHAR2(100)
I can't change the datatype. How should I do this?
As the message, says, your date format is incorrect. It should be
'DD-MON-YYYY HH12:MI AM'
See the manual for the format elements. For example:
SELECT TO_DATE('07-Jan-2018 12:30 AM', 'DD-MON-YYYY HH12:MI AM')
FROM dual
Output:
2018-01-07T00:30:00Z

Insert with the current date and time

INSERT INTO PUBLIC.PROJECT(ID, LIBELLE, DESCRIPTION, STATUT, DATE_CREATION, DATE_MODIFICATION, USER_CREATEUR_ID, REFERENT_ID, DATE_DEBUT, DATE_FIN, TYPE, STATUT_PROJET) VALUES
(2651, 'Projet 1', NULL, 'START', DATE '2019-11-15', DATE '2019-11-15', NULL, 952, TIMESTAMP '2019-11-14 23:00:00', TIMESTAMP '2019-12-14 23:00:00', NULL, NULL);
I want to modify this request and use current_timestamp to have the current date and the current date with time
Example: 2019-11-19 23:00:00
I try to do that : date (current_timestamp + time '23:00') but i have some errors.
Someone have idea ?
Use current_date instead:
current_date + time '23:00'
And you don't need to cast the result, use it as written.

how to add AM or PM in PostgreSQL in date format itself?

select to_date(to_char(sysdate,'yyyy-mm-dd hh12:mi:ss AM'),
'yyyy-MM-dd HH12:MI:SS AM') from dual;
This works fine in oracle but not in Postgres.
Instead of sysdate you should use current_timestamp, or now():
SELECT TO_CHAR(current_timestamp, 'yyyy-mm-dd hh12:mi:ss AM')
A table for add AM or PM format itself.
create table date_tbl
(
id serial,
Date_time varchar (30) default TO_CHAR(current_timestamp, 'yyyy-mm-dd hh:mi:ss AM')
)
Try the below function to accept time stamp with AM/PM over PostgreSQL
TO_CHAR(SYSDATE, 'MM/DD/YYYY HH12:MI:SS AM')

Oracle: How to filter by date and time in a where clause

How can I do this:
select *
from tableName
where SESSION_START_DATE_TIME > To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi' )
SESSION_START_DATE_TIME is in the format '12/01/2012 13:16:32.000'
I tried where To_Date (SESSION_START_DATE_TIME, 'DD-MON-YYYY hh24:mi') > To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi' )
but no matter what I try I get the error:
SQL command not properly formed
In the example that you have provided there is nothing that would throw a SQL command not properly formed error. How are you executing this query? What are you not showing us?
This example script works fine:
create table tableName
(session_start_date_time DATE);
insert into tableName (session_start_date_time)
values (sysdate+1);
select * from tableName
where session_start_date_time > to_date('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi');
As does this example:
create table tableName2
(session_start_date_time TIMESTAMP);
insert into tableName2 (session_start_date_time)
values (to_timestamp('01/12/2012 16:01:02.345678','mm/dd/yyyy hh24:mi:ss.ff'));
select * from tableName2
where session_start_date_time > to_date('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi');
select * from tableName2
where session_start_date_time > to_timestamp('01/12/2012 14:01:02.345678','mm/dd/yyyy hh24:mi:ss.ff');
So there must be something else that is wrong.
If SESSION_START_DATE_TIME is of type TIMESTAMP you may want to try using the SQL function TO_TIMESTAMP. Here is an example:
SQL> CREATE TABLE t (ts TIMESTAMP);
Table created.
SQL> INSERT INTO t
2 VALUES (
3 TO_TIMESTAMP (
4 '1/12/2012 5:03:27.221008 PM'
5 ,'mm/dd/yyyy HH:MI:SS.FF AM'
6 )
7 );
1 row created.
SQL> SELECT *
2 FROM t
3 WHERE ts =
4 TO_TIMESTAMP (
5 '1/12/2012 5:03:27.221008 PM'
6 ,'mm/dd/yyyy HH:MI:SS.FF AM'
7 );
TS
-------------------------------------------------
12-JAN-12 05.03.27.221008 PM
Put it this way
where ("R"."TIME_STAMP">=TO_DATE ('03-02-2013 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
AND "R"."TIME_STAMP"<=TO_DATE ('09-02-2013 23:59:59', 'DD-MM-YYYY HH24:MI:SS'))
Where
R is table name.
TIME_STAMP is FieldName in Table R.
Try:
To_Date (SESSION_START_DATE_TIME, 'MM/DD/YYYY hh24:mi') >
To_Date ('12-Jan-2012 16:00', 'DD-MON-YYYY hh24:mi' )
Obviously '12/01/2012 13:16:32.000' doesn't match 'DD-MON-YYYY hh24:mi' format.
Update:
You need 'MM/DD/YYYY hh24:mi:ss.ff' format and to use TO_TIMESTAMP instead of TO_DATE cause dates don't hold millis in oracle.