var epochtime = "1610950344";
var datevar = new Date(epochtime *1000);
var mytime = datevar.toISOString().slice(0, 19).replace('T', ' ');
console.log("DATEEEEEEEEEE : "+mytime);
const query = `INSERT INTO MYTIME (MY_TIME, NAME) VALUES ('${mytime}', 'ANN')`;
updateDB(query); //Custom function to create a connection and insert the data into the table
MY_TIME is defined as TIMESTAMP in mysql schema. The above query is working except for the MY_TIME field. When mytime gets inserted, it changes the values to "0000-00-00 00:00:00" in mysql. Why is this happening? Is there anything wrong with the data conversion. Please help
You can simply query like this:
INSERT INTO MYTIME (MY_TIME, NAME) VALUES (FROM_UNIXTIME(${epochtime}), 'ANN')
Related
I want to write a validation in such a way that my query should result true if same CCN and date already exist in DB, from JSON and IN DB datetime is saved in "yyyy-MM-dd HH:mm:ss", but in my validation i need to take only date[yyyy-MM-dd] and compare.
I am new to spring boot
This is what i have in my model class
#JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
#Temporal(TemporalType.DATE)
private Date datetime;
Repository for my class
#Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM TABLEX c WHERE c.ccn = :ccn and c.datetime= :datetime")
boolean isExistbyCcnAndDate(#Param("ccn") String conveyancereferencenumber, #Param("datetime") #DateTimeFormat(pattern = "yyyy-MM-dd") Date date);
When i try this it gives result as always false.
This is how i am passing values to repository
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(model.getDatetime());
convertedDate = sdf.parse(dateString);
isExistCcnAndDate=repository.isExistbyCcnAndDate(model.getCcn(),convertedDate);
Check out my sqlfiddle.
http://sqlfiddle.com/#!9/5305eb/3
Using sql fiddle this will work:
Create shema:
create table c(datetime DATETIME, ccn TEXT)
insert into c (datetime, ccn) values ('2020-01-01 10:10:50' , 'a');
Try to read and filter step by step:
select * from c;
select * from c where c.datetime = '2020-01-01 10:10:50';
select * from c where date(c.datetime)= date('2020-01-01');
As I already said, whether you can use date(), DATE() or whatever depends on your server, sqlfiddle is MySQL.
I want to put this format data on my table: 2018-12-04T13:05:00-00:00
This should be done with a query:
$uPqr = $conn->query("UPDATE table SET dateModified = "the date goes here" WHERE id = 25");
I don't want to write the date on the query, i want to know if there's a function like NOW() or time() that do it automatically.
If you want the current time -- on the server -- then just use now():
$uPqr = $conn->query("UPDATE table SET dateModified = now() WHERE id = 25");
I need to update a json object for the 'lastUpdated' field with the current time in Epoch. I am doing the following but it is failing:
UPDATE data SET data = data || '{"lastUpdated": extract(epoch from current_timestamp) }'
WHERE dtype='Employee' and data->>'id' = '1234';
The following itself doesn't work:
UPDATE data SET data = data || '{"lastUpdated": current_timestamp }'
WHERE dtype='Employee' and data->>'id' = '1234';
I am getting 'invalid input syntax for type json'.
Please note, the following works when i set a custom epoch time:
UPDATE data SET data = data || '{"dateAdded":1447502107000 , "lastUpdated":1447502107000}' WHERE dtype='Employee' and data->>'id' = '1234';
First, you should alter the data type of the column data to jsonb, as the concatenation operator || does not work with json.
alter table data alter data type jsonb;
Then use the function jsonb_build_object():
UPDATE data SET data = data || jsonb_build_object('lastUpdated', extract(epoch from current_timestamp))
WHERE dtype='Employee' and data->>'id' = '1234';
If you do not want to change the column type, you have to use casting:
UPDATE data SET data = data::jsonb || jsonb_build_object('lastUpdated', extract(epoch from current_timestamp))
WHERE dtype='Employee' and data->>'id' = '1234';
Update
Choose one of the two variants to round or get rid of precision of timestamp:
select
extract(epoch from current_timestamp),
replace(extract(epoch from current_timestamp::timestamp(3))::text, '.', ''),
concat(extract(epoch from current_timestamp::timestamp(0))::text, '000')
date_part | replace | concat
------------------+---------------+---------------
1533560841.14562 | 1533568041146 | 1533568041000
(1 row)
Alternatively:
select
extract(epoch from current_timestamp),
extract(epoch from current_timestamp(3))* 1000 as v1,
extract(epoch from current_timestamp(0))* 1000 as v2
This is not working because extract is a function and by using it inside '', you are basically sending a string, which returns you timestamp
UPDATE data SET data = data || '{"lastUpdated": extract(epoch from
current_timestamp) }'
WHERE dtype='Employee' and data->>'id' = '1234';
Meanwhile this works because you are storing the way it is meant to store
UPDATE data SET data = data || '{"dateAdded":1447502107000 ,
"lastUpdated":1447502107000}' WHERE dtype='Employee' and data->>'id' = '1234';
Though I would say to try Klin's way, but the solution to what you are asking can also be (make sure you change the data type to JSONB):
UPDATE data SET data = data || '{"lastUpdated":' || extract(epoch from
current_timestamp)::text || '}'
WHERE dtype='Employee' and data->>'id' = '1234';
I have to update table which have column data type as integer, but I have an input as a datetime. this is query that i hve writen
UPDATE T_SCH_ETAX_TEMP SET CURRTIME = UNIX_TIMESTAMP
(TIMEDIFF("(SELECT DATE_FORMAT(?,'%H:%i:%s') TIMEONLY)", "(SELECT
DATE_FORMAT(CURRENT_TIMESTAMP,'%H:%i:%s') TIMEONLY)"), LENGTHTIME =
UNIX_TIMESTAMP(TIMEDIFF("(SELECT DATE_FORMAT(?,'%H:%i:%s')
TIMEONLY)", "(SELECT DATE_FORMAT(CURRENT_TIMESTAMP,'%H:%i:%s')
TIMEONLY)")
any one can help how to convert timestamp result as an integer so I can update the table using SSIS
I could be wrong, please forgive me if I am, but you seem to want something simpler:
UPDATE T_SCH_ETAX_TEMP
SET CURRTIME = UNIX_TIMESTAMP(
TIMEDIFF(TIME(?),
TIME(CURRENT_TIMESTAMP))),
LENGTHTIME = UNIX_TIMESTAMP(
TIMEDIFF(TIME(?), TIME(CURRENT_TIMESTAMP)));
Which would only require 2 parameters ('ss');
If this is the case, in can be simplified to:
UPDATE T_SCH_ETAX_TEMP
SET CURRTIME = UNIX_TIMESTAMP(
TIMEDIFF(?,
CURRENT_TIMESTAMP)),
LENGTHTIME = UNIX_TIMESTAMP(
TIMEDIFF(?, CURRENT_TIMESTAMP));
if you pre-format your parameter strings
Currently this is my code, which inserts the new_item correctly
var new_item = {id_user: id, id_restaurant: id_rest, type: 3, date: date};
connection.query("INSERT INTO table SET ?", [new_item], function(err, results) {
if(err){
...
}
...
}
But now I don't want to send the date with it, I want to use SET ? and do something likedate = NOW() basically I want to insert this:
var new_item = {id_user: id, id_restaurant: id_rest, type: 3};
and use date = NOW()-INTERVAL 6 hour to assign the date.
EDIT: I posted I only wanted Now() but actually I want to set Interval it before inserting.
I think it is better to use moment.js.
You can use Date.now which returns current timestamp(milliseconds), so make sure to format it. The code is as follows:
var a = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
Please let me know if this works out
new Date().toISOString().slice(0,10) + " " + new Date().toISOString().slice(11, 19)
same as 'YYYY-MM-DD HH:mm:ss'
Using NOW() is executed by the database interpreter and sets the database time.
You could create a new variable to use in your SQL statement and set the wanted content to that one.
var new_item1 = {id_user: id, id_restaurant: id_rest, type: 3, date: date};
var new_item2 = {id_user: new_item1.id_user, ...};
connection.query("INSERT INTO table SET ?", [new_item2], function(err, results) {
if(err){
...
}
...
}
Then you could configure your column to set the NOW() timestamp as default.