how to update datetime colum (mssql) with formatted current datetime in querydsl - sql-server

I want to update datetime column with current datetime.
but I've got an error at QAa.aaa.visitDt line.
The data type of QAa.aaa.visitDt is DateTimePath<java.time.LocalDateTime>
```
Date date = new Date();
String updDt = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
Long ret = queryFactory.update(QAa.aaa)
.set(QAa.aaa.visitCnt, QAa.aaa.visitCnt.add(1))
.set(QAa.aaa.visitDt, Expressions.dateTimePath(LocalDateTime.class, updDt))
.where(QAa.aaa.aNo.eq(aNo))
.execute();
Error Message is below.
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ~
column 90 [update ~~
set aaa.visitCnt =aaa.visitCnt + ?1, aaa.visitDt= 2022-09-12 13:24:36.330
where aaa.aNo = ?3

Related

Snowflake Update table error: Unsupported type for binding argument

I'm trying to update a table via a stored procedure based on the Log_Id passed and update the status and insert today's date timestamp in the table (when the table is updated).
Stored procedure:
...
CREATE OR REPLACE PROCEDURE update_table(P_ETL_STATUS_CODE VARCHAR,P_LOG_ID FLOAT)
RETURNS FLOAT
LANGUAGE JAVASCRIPT
AS
$$
var sql_command1 = "UPDATE ETL_EXECUTION_STATUS_LOG SET ETL_STATUS_CODE = :1,ETL_EXEC_END_TIME = :2 WHERE LOG_ID = :3";
var create_stmt1 = snowflake.execute({sqlText : sql_command1,
binds: [P_ETL_STATUS_CODE,(new Date()),P_LOG_ID].map(function(x){return x === undefined ? null : x})
}
);
return P_LOG_ID;
$$
;
...
call update_table('Test77',47)
But, I'm getting the following error:
Error message: Execution error in stored procedure UPDATE_TABLE: Unsupported type for binding argument Sun Apr 05 2020 18:39:31 GMT-0700 (PDT) At Snowflake.execute, line 3 position 30
The table already contains Log_Id 47. Column 'ETL_EXEC_END_TIME' is timestamp_ntz in table.
Can you suggest where I'm going wrong?
Cheers
JavaScript date format is not compatible with Snowflake's date format. As you can see from the message, JavaScript sends this:
"Sun Apr 05 2020 18:39:31 GMT-0700 (PDT)"
You may convert this to more common date format:
https://stackoverflow.com/a/21482470/12550965
I see that you want to record timestamp. So why don't you use CURRENT_TIMESTAMP command?
CREATE OR REPLACE PROCEDURE update_table(P_ETL_STATUS_CODE VARCHAR,P_LOG_ID FLOAT)
RETURNS FLOAT
LANGUAGE JAVASCRIPT
AS
$$
var sql_command1 = "UPDATE ETL_EXECUTION_STATUS_LOG SET ETL_STATUS_CODE = :1,ETL_EXEC_END_TIME = CURRENT_TIMESTAMP::TIMESTAMP_NTZ WHERE LOG_ID = :2";
var create_stmt1 = snowflake.execute({sqlText : sql_command1,
binds: [P_ETL_STATUS_CODE,P_LOG_ID].map(function(x){return x === undefined ? null : x})
}
);
return P_LOG_ID;
$$
;

Hibernate HQL query error: The conversion of a varchar data type to a smalldatetime

I try to get result from Hibernate HQL query:
from StopsRegister s where s.startTime>='2018-04-25 07:59:00.0' and s.endTime<='2018-04-26 07:59:00.0'
from StopsRegister s where s.startTime between '2018-04-25 08:42:00' and '2018-04-26 08:42:00'
The issue starts when a add a secound date: and s.endTime<='2018-04-26 07:59:00.0' With only one value everything works correct.
But get the SQL Error:
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
That is the way i get the date values:
Timestamp.valueOf(LocalDateTime.of(toDatePicker.getDate(), toTimePicker.getTime()));
data types is: java.sql.Timestamp;
database: MS SQL Server.
In me opinion date format is correct, I do not understan this error.
That solve the issue:
Session session = HibernateUtil.getSessionFactory().openSession();
Query q = session.createQuery("from StopsRegister s where s.startTime >= :from and s.startTime <= :to");
q.setTimestamp("from", from);
q.setTimestamp("to", to);
stopRegisterList = (ArrayList<StopsRegister>) q.list();

How to add days to date time in Salesforce Apex?

Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

Camel Bindy Date format Unmarshalling issue

I ran into an issue with Apache Camel Bindy data format for the Date field parsing from a CSV file.
Date in the CSV is 02/11/2015 03:34:49 PM
Format in the Bindy Class annotated as
#DataField(pos = 8,pattern="MM/dd/yyyy hh:mm:ss a")
private Date time;
Getting below exception
java.lang.IllegalArgumentException: Date provided does not fit the
pattern defined, position: 8, line: 1 at
org.apache.camel.dataformat.bindy.BindyCsvFactory.bind(BindyCsvFactory.java:213)
at
org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat.unmarshal(BindyCsvDataFormat.java:185)
at
org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:67)
It works if the date in the CSV is given as 2/11/2015 03:34:49 PM, with no preceeding 0 in the Month field.
I am using Camel 2.14.1.
Am I doing anything wrong here ?
From the source code of DatePatternFormat.java :
public Date parse(String string) throws Exception {
Date date;
DateFormat df = this.getDateFormat();
ObjectHelper.notNull(this.pattern, "pattern");
// Check length of the string with date pattern
// To avoid to parse a string date : 20090901-10:32:30 when
// the pattern is yyyyMMdd
if (string.length() <= this.pattern.length()) {
// Force the parser to be strict in the syntax of the date to be
// converted
df.setLenient(false);
date = df.parse(string);
return date;
} else {
throw new FormatException("Date provided does not fit the pattern defined");
}
}
It check that parsed string is smaller than the pattern.
Then changing your pattern to MM/dd/yyyy hh:mm:ss aa should fix your problem.

How can I convert a date to this particular string representation?

LblExpirydate.Text = dataReader(0).ToString()
Output in asp.net form : 01/05/2013 12:00:00 AM
I want to change format to (01/05/2013)
Notes : My database
Column : Expirydate
data type : Date
You can format the string using .ToString() in several different ways:
dataReader(0).ToString("dd/MM/yyyy");
dataReader(0).ToString("d");
The second option is better as it will format using the current locale. See this MSDN article for more info:
http://msdn.microsoft.com/en-gb/library/az4se3k1.aspx
DateTime thisDate1 = DateTime.Now;
Label1.Text =thisDate1.ToString("MM/dd/yyyy") ;
output:
03/30/2013
DateTime thisDate1 = DateTime.Now;
Label1.Text =thisDate1.ToString("MM-dd-yyyy") ;
output:
03-30-2013
DateTime thisDate1 = DateTime.Now;
Label1.Text =thisDate1.ToString("MM,dd,yyyy") ;
output:
03,30,2013
DateTime thisDate1 = DateTime.Now;
Label1.Text =thisDate1.ToString("MMMM,dd,yyyy") ;
output:
March,30,2013
and so on...

Resources