I’m trying to run an insert query, with the iAddValue.text being the string “0447” (for example). When it gets inserted into the database, the value “447” gets inserted. This only happens if I use prepared statements, not if I turn prepared statements off. It’s identical to the issue I listed above, but that issue is marked as fixed. So I’m not sure if I need to change my query, or if it isn’t really fixed. Here’s the query I’m using (though I’ve tried several different iterations of it):
insert into keysandvalues(name, key, value) values ({{Clients.selectedRow.name}},{{iAddKey.text}}, {{iAddValue.text}}::varchar);
This change of typecasting a value in Prepared Statements has only been made for Postgres database today. What database are you using? I could raise an issue and this can be tackled for the others as well.
@trebuchet yes, this requires the use of prepared statements to be off since the underlying driver tries to identify the value as a number instead of a string
Ok, thanks @Nikhil - as workaround for those that want to use Prepared Statements while still using leading zeros in strings like this you can use LPAD()
To elaborate on the question I posed a few days ago, I have a field that is a two character string. Here are some examples of values:
03
09
12
13
14
H8
J1
Z1
...
If I use a prepared statement, ‘03’ and ‘09’ will be inserted/updated as ‘3’ and ‘9’ respectively.
In Postgres, I could have simply done this:
UPDATE form
SET code = {{input_code.selectedOptionValue}}::varchar
WHERE id = {{appsmith.store.payrollFormId}}
But ::varchar doesn’t work in MS SQL. Is there another way to explicitly typecast? I don’t want my insert/update scripts to be filled with messy CASE statements.
I’ve tried both of your suggested solutions, but neither work. If there is a leading zero in the value, it is stripped before being sent to MS SQL. Appsmith typecasts the value as a number before the SQL script is even executed.
Hi @jdufault
To be precise CONAT('0', {{Input.text}}) works when there is only one leading zero in your column. Can you please tell us how many zeros you might need to add as prefix? Can you try with the solution using LPAD as mentioned in this comment?
Thanks for your reply. LPAD is not a valid function in MS SQL Server, so that won’t work.
My point is that I do not necessarily know if or how many leading zeros will be needed. This is a two-character alphanumeric field. In Appsmith, the values that need a leading zero already have one. it’s just that Appsmith converts them to a number and removes the leading zero prior to execution.
Yes, I totally get your point. That’s an issue in Appsmith. This soon will get resolved. What I have proposed is a workaround for this (Prepared Statemtnt: OFF).
By the way, LPAD doesn’t come out-of-the-box in SQL-Server. You can check this Stackoverflow post.
But this should work when you turn off the prepared-statement.
Yes, I am aware that I can turn off Prepared Statements. But understandably, I want to try every available option before doing that.
I will look at adding my own LPAD function in SQL Server as a workaround. But ultimately I’m really looking forward to seeing an actual resolution to this problem. Imagine, for example, a field that doesn’t have a fixed length and some values contain leading zeros.