Add days to date in SAP HANA stored procedure - stored-procedures

I need to add days to date in HANA stored procedure, but I am getting error message
ERROR WHILE parsing DATE/ TIME
I use this statement where p_end_date is parameter of my stored procedure.
v_end_date_plus := add_days (TO_DATE(' || p_end_date || ' , 'YYYY-MM-DD' ), 90)
Is there is any other way or what I am doing wrong in it ?

Even though you didn't post what error you receive, I guess that the problem in your code is the way you referenced your input variable.
v_end_date_plus := add_days ( :p_end_date , 90);
With the colon (:) in front of the parameter you should be able to use it without having to cast it into a different data type.

#LarsBr. is correct that you need a colon (:) to reference the variable, and that if it is really a DATE type, you don't need to convert TO_DATE again.
But additionally, in your example you have some mixup with quotes and concatenation that makes me think that you actually want to construct some character string using p_end_date. This would need conversion to a date first:
p_end_date := '2016-05-03'; -- for example
v_end_date_plus := add_days(TO_DATE( :p_end_date , 'YYYY-MM-DD' ), 90);
The part ' || p_end_date || ' in your example also looks a bit like the whole code was actually part of string to be used in EXEC or similar. If that's the case, you need to have escaped single-quotes for both parameters, e.g.
exec 'v_end_date_plus := add_days(TO_DATE(''' || :p_end_date || ''', ''YYYY-MM-DD'' ), 90)';

p_end_date should be a varchar field, or the appropriate string literal used in your technology. It should not be enclosed in quotes.
v_end_date_plus := add_days (TO_DATE(p_end_date , 'YYYY-MM-DD' ), 90)
EXPLANATION USING ORACLE AS REFERENCE :
In Oracle database, the default date format is dd-MON-RR or dd-MON-YYYY.
So, if I use correct date format to p_end_date variable, I am able obtain output.
However, if I diverge from this default format,my attempt would error out.
So, if I want the flexibility to redefine p_end_date in format of my choice, and not as per default settings, it should be a String literal.(varchar in Oracle ).
EDIT:
The essence of this answer was just to suggest that the variable should be passed as a varchar. Borrowing from Lars Br's suggestion below to modify the p_end_date variable's syntax:
v_end_date_plus := add_days (TO_DATE(:p_end_date , 'YYYY-MM-DD' ), 90)

Related

Delphi - Formating a multiple %s string with one argument

When I try to format a string such as '%s%s' using a line of code like so:
format('%s%s', [x]);
I get an exception because you can't have multiple '%s' without using an array with the same amount of arguments such as:
format('%s%s', [x,x]);
However, I don't know how many '%s' I will have to format and therefore I don't know how long the array would have to be. I also only want '%s' assigned to only 1 value.
Is there a way in which you can use multiple '%s' and assign them all to the same index?
As described in the documentation you can use an index specifier to identify the argument by a zero based index. The index specifier is written immediately after the % and is followed by a :.
Your example would be:
Format('%0:s%0:s', [x])
MyStr := StringReplace('%s%s', '%s', x, [rfreplaceALL]);

Figuring out what this Trim() function is doing?

I have a constraint on a transformer with this:
Trim(CollectFrom.collect_from,"-","A")<=TheDate
Here is what collect_from looks like:
'2017-02-27'
And here is what TheDate looks like:
'20170227'
I am unsure exactly how this Trim() function works. My first guess is that it is giving it the same format as 'TheDate' however I don't understand the "A" argument. Could somebody explain it?
The manual page for TRIM() says that shouldn't work.
When I try to run what you show, I get errors:
SQL[2405]: select trim('2017-02-01', '-', 'A') from dual;
SQL -674: Routine (trim) can not be resolved.
SQLSTATE: IX000 at /dev/stdin:1
SQL[2406]: select trim('2017-02-01', '-') from dual;
SQL -674: Routine (trim) can not be resolved.
SQLSTATE: IX000 at /dev/stdin:2
The manual says you need TRIM({BOTH|LEADING|TRAILING} [char] FROM source):
SQL[2407]: select trim(both '-' from '2017-02-12') from dual;
2017-02-12
SQL[2408]: select trim(both '-' from '2017-02-12-') from dual;
2017-02-12
SQL[2409]: select trim(both '-' from '-2017-02-12-') from dual;
2017-02-12
SQL[2410]:
(The SQL command interpreter used is my SQLCMD.)
There's a chance that someone has defined a TRIM function that takes three arguments — that's not detectable from here. You'd have to look in the system catalog of your database to find that.
OTOH, that doesn't seem to be allowed, either:
SQL[2411]: create function trim(a varchar(10), b varchar(20), c varchar(30)) returning varchar(64);
return trim(leading from a) || ' ' || trim(both from b) || ' ' || trim(trailing from c);
end function;
SQL -9710: Overloading of built-in functions is not allowed.
SQLSTATE: IX000 at /dev/stdin:8
SQL[2412]:
Informix 11.50 has build in trim() function: https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_1556.htm But it looks little different from trim() in your question, so I think you use trim() function build by some user.
Normally trim() takes care only at chars at beginning or at end of the string, but in your example trim() had to remove - chars that are in the middle of the string. I guess that last argument A tells trim() to remove all such characters from source string.
To find out what trim() function really do you must find it source. You can do it with some GUI database tools like SQirreL SQL Client (it uses JDBC), you can do it with dbschema Informix utility or with my Python/Jython program that uses ODBC/JDBC: https://code.activestate.com/recipes/576621-dump-informix-schema-to-text
It would be helpful to tell us what version of Informix you use.
Trim(CollectFrom.collect_from,"-","A")
Here A means to Remove all the occurrences of '-' from the input string CollectFrom.collect_from . Hence You are getting '20170227' . Here is the document which would give further more information on Trim function.
https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_11.5.0/com.ibm.swg.im.iis.ds.parjob.dev.doc/topics/r_deeref_String_Functions.html

Operator not applicable to this operand when I try to make filter based on date

I am kind of new to Delphi and Query and before I posted this question, I searched everywhere that related to my problem but it seems I was unable to find anything that could help me solve my problem.
I found this error message that operator not applicable to this operand type with this code:
SQL.Text:='SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE'
+QuotedStr('%'+cxButtonEdit1.Text+'%') 'AND MONTH(Tgl)'=
+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date)')+ 'AND YEAR(Tgl)'=
+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date));
How do I solve this error?
Your code has several flaws :
You are mixing some symbols.
You missed a space after the LIKE sentence
You are using the FormatDateTime function without a valid format to get the month or year of the date.
Finally you are not using parameters.
Try this sample (you must replace the ADOQuery1 for the real Query component name)
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE :Nik');
ADOQuery1.SQL.Add('AND MONTH(Tgl) = :MonthTgl');
ADOQuery1.SQL.Add('AND YEAR(Tgl) = :YearTgl');
ADOQuery1.Parameters.ParamByName('Nik').Value := '%'+cxButtonEdit1.Text+'%';
ADOQuery1.Parameters.ParamByName('MonthTgl').Value := MonthOf(dtp1.Date);
ADOQuery1.Parameters.ParamByName('YearTgl').Value := YearOf(dtp1.Date);
ADOQuery1.Open;
You are missing a + operator, and have some quotes mixed up. Instead of:
SQL.Text:='SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE'
+QuotedStr('%'+cxButtonEdit1.Text+'%') 'AND MONTH(Tgl)'=
+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date)')+ 'AND YEAR(Tgl)'=
+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date));
I presume you mean
SQL.Text:='SELECT COUNT(Nik) FROM Absent WHERE Nik LIKE'
+QuotedStr('%'+cxButtonEdit1.Text+'%') +' AND MONTH(Tgl)='
+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date))+ ' AND YEAR(Tgl)='
+QuotedStr(FormatDateTime('yyyy-mm-dd',dtp1.Date));
Note also that you must stop building queries this way. You are opening yourself up to SQL injection attacks. Use parameterised queries instead.

Formatting a string in delphi

I want to update a text like "Updating report (1 0f 5)". I thought format function will help me to do that. I want something like this
string := Format('Updating report ( %d of %d, [1], [2])', loop, count );
but it is not possible. I have an option to have loop and count stored in a string and concatenate everything. But is there any other way to achieve what i want?
Your syntax is wrong. The second parameter to the Format is an open array containing the arguments. So you need to wrap your list of arguments in what is known as an open array constructor.
An open array constructor is a sequence of expressions separated by commas and enclosed in brackets.
So, write the code like this:
str := Format('Updating report (%d of %d)', [loop, count]);

How to modify a generator value

I want to modify a generator value in Delphi with Firebird 2.5. The Statement is:
ALTER SEQUENCE GEN_NAME RESTART WITH value . What I want is that the value not to be a number but a variable or a parameter like:
ALTER SEQUENCE GEN_TELAGENT_ID RESTART WITH val; where val get a different integer value I don't know yet.
I hope I made myself understood. Sorry for my not so good English.
Thanks for the answers.
use a parameter, with your preferred components it should work.
myQuery.SQL.Text := 'alter sequence gen_telagent_id restart with :val';
myQuery.Params.ParamByName('val').AsInteger := val;
myQuery.ExecSQL();
Actual syntaxis may vary depending on the components you use to connect to Firebird.

Resources