Wrong datetime value for now() function - spring

I have a question. I'm writing a program in springu and jpa / hibernate. database which i use is PostgreSQL. I have simple table with 2 column: first with type "timestamp with time zone" and second with type "timestamp without time zone". In my model those column have "LocalDateTime" type. I use nativeQuery from JpaRepository like this:
#Modifying
#Query(value = "INSERT INTO public.tempor(id, date1, date2) VALUES (2, now(), now());", nativeQuery = true)
void saveTransaction();
After inserting this to database first column have correct value from datebase datetime, but second is set as my localhost datetime. Why and how to solve this problem? Thanks for helps.

You can use CURRENT_TIMESTAMP for example:
INSERT into tempor (id,date1)
VALUES (1, current_timestamp);
second option, you can make the default value for your column as now():
alter table tempor alter date1 set default now();

Related

how to set column default value like current_timestamp in clickhouse?

does anyone know how to use a default value like current_timestamp in mysql when creating a clickhouse table? The now() udf is dynamic, instead of the time the row inserted, it is always the current time, it changes when select.
Here is my table:
CREATE TABLE default.test2 (
`num` UInt32,
`dt` String,
`__inserted_time` DateTime DEFAULT now()
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/test2', '{replica}')
PARTITION BY dt
ORDER BY dt
SETTINGS index_granularity = 8192
I want the __inserted_time column value generated automatically thus I don't have to specify it in the insert into test2 (num,dt) values (1,'20191010')
my mistake, DEFAULT now() actually works

How to get the value of clob data which has date field and compare with timestamp in oracle

I have a table named masterregistry and it contains all the info and business logic in it and the data type of the colum is clob
desc master_registy:
id number not null,
name varchar2(100),
value clob
select value from master_registry where name='REG_DATE';
o/p
11-10-17
This date is common across all the business logic, I need to query my tables which has ,
desc get_employee
====================
id number not null,
first_name varchar2(100),
last_name varchar2(100),
last_mod_dt timestamp
Now I need to get all the values from the get_employee whose last_mod_dt should be greater than the value of master_registry where name='REG_DATE'.The value in the latter table is clob data, how to fetch and compare the date of a clob data against the timestamp from another table. Please help.
Maybe you need something like this.
SELECT *
FROM get_employee e
WHERE last_mod_dt > (SELECT TO_TIMESTAMP (TO_CHAR (VALUE), 'DD-MM-YY')
FROM master_registy m
WHERE m.id = e.id);
DEMO
Note that i have used the column value directly in TO_CHAR. You may have to use TRIM,SUBSTR or whatever required to get ONLY the date component.

How to insert date in Oracle 10g xe?

I did the following:
CREATE TABLE BOOK(
BOOK_ID VARCHAR(4) PRIMARY KEY,
ISBN_10 VARCHAR(10),
TITLE VARCHAR(50),
CATEGORY VARCHAR(25),
PRICE DECIMAL(6,2),
BINDING VARCHAR(2),
PUB_DATE DATE,
AUTHOR_ID SMALLINT,
PUBLISHER_ID SMALLINT
);
2.
ALTER SESSION SET nls_date_format = 'DD-MM-YYYY HH24:MI:SS';
3.
INSERT INTO BOOK
VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S',**'26-01-1991'**,13,103);
It gave an error:
ORA-01843: not a valid month
However if do the following there is no problem:
Query:
INSERT INTO
BOOK
VALUES('4','123459','INTRODUCTION TO Small Talk','IT',157.00,'S','**26-JAN-1991**',13,103);
Can anyone explain why?
You want to read the Datetime Literals section of the manual. Alternatives are:
Use a date literal: DATE '1991-01-26'
Convert from string: TO_DATE('26-01-1991', 'DD-MM-YYYY')
If you set NLS_DATE_FORMAT you can omit TO_DATE()'s second argument but not skip the function entirely.
See also: Datetime Format Models
It worked for me. That implies that your alter statement did not work for some reason. However, it is not good practice to assume a specific date format on a system when using literals. Instead, cast the literal with a format mask on the insert, such as:
INSERT INTO BOOK
VALUES('4','123459','INTRODUCTION TO Small Talk','IT',157.00,'S',
to_date('26-JAN-1991','DD-MON-YYYY'), 13,103);
(or whatever format you require as a literal input value)
This because in your database, nls_date_time is set to 'DD-MON-RRRR' format, you can check this using
select * from V$NLS_PARAMETERS
In this query
INSERT INTO BOOK VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S','26-01-1991',13,103);
date format is 'dd-mm-rrrr', change the statement as
INSERT INTO BOOK VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S',to_date('26-01-1991', 'dd-mm-rrrr'),13,103);
and it will also run since you have now provided the date format.
When you set
ALTER SESSION SET nls_date_format = 'DD-MM-YYYY HH24:MI:SS';
then your insert must match this format, i.e.
INSERT INTO BOOK VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S',
'26-01-1991 00:00:00'
,13,103);
However, the more secure way is to use date literals or TO_DATE function.
You just need to use
INSERT INTO TABLE_NAME VALUES('07-JAN-96');
Oracle always takes "DD-MMM-YY" format. You should write your month as the first 3 characters of month name.

how to do in hibernate like order by to_date(ADD_DATE_CREATED,'DD-MM-YY') desc

i want to add criteria in hibernate for order by date.
Here date description in db as
vrhCreatedDate varchar2(20)
Here i have date with varchar datatype in database .
i m doing order by using
order by to_date(ADD_DATE_CREATED,'DD-MM-YY') desc
how to add criteria like below for order by date itself ?
criteria.addOrder(Order.desc("to_date({alias}.vrhCreatedDate, 'DD-MM-YY')")); //need to parse varchar column in date when pass order by column in hibernate.
Parse order by column with string to date field.
You can use the following statment to order by your date field
criteria.addOrder(Order.desc("your Field name"));

how to insert date and time in oracle?

Im having trouble inserting a row in my table. Here is the insert statement and table creation. This is part of a uni assignment hence the simplicity, what am i doing wrong? Im using oracle SQL developer Version 3.0.04.'
The problem i am having is that it is only inserting the dd/mon/yy but not the time. How do i get it to insert the time as well?
INSERT INTO WORKON (STAFFNO,CAMPAIGNTITLE,DATETIME,HOURS)
VALUES ('102','Machanic Summer Savings',TO_DATE('22/April/2011 8:30:00AM','DD/MON/YY HH:MI:SSAM'),'3')
;
CREATE TABLE WorkOn
(
StaffNo NCHAR(4),
CampaignTitle VARCHAR(50),
DateTime DATE,
Hours VARCHAR(2)
)
;
Thanks for the help.
EDIT: This is making no sense, i enter just a time in the field to test if time is working and it outputs a date WTF? This is really weird i may not use a date field and just enter the time in, i realise this will result in issues manipulating the data but this is making no sense...
You can use
insert into table_name
(date_field)
values
(TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'));
Hope it helps.
You are doing everything right by using a to_date function and specifying the time. The time is there in the database. The trouble is just that when you select a column of DATE datatype from the database, the default format mask doesn't show the time. If you issue a
alter session set nls_date_format = 'dd/MON/yyyy hh24:mi:ss'
or something similar including a time component, you will see that the time successfully made it into the database.
Try this:
...(to_date('2011/04/22 08:30:00', 'yyyy/mm/dd hh24:mi:ss'));
Just use TO_DATE() function to convert string to DATE.
For Example:
create table Customer(
CustId int primary key,
CustName varchar(20),
DOB date);
insert into Customer values(1,'Vishnu', TO_DATE('1994/12/16 12:00:00', 'yyyy/mm/dd hh:mi:ss'));
create table Customer(
CustId int primary key,
CustName varchar(20),
DOB date);
insert into Customer values(1,'kingle', TO_DATE('1994-12-16 12:00:00', 'yyyy-MM-dd hh:mi:ss'));

Resources