Passing date string to a function in an ibatis query - ibatis

I am passing a date string to an ibatis query which is used for comparison. However, I am getting an error
SELECT PATIENT_SUFFIX_NM,
INTRPT_CLM_TXT,
DAYS(DATE(#{batch_dt_ud})) - DAYS(INTRPT_CLM_ENTR_TS) AGE_OF_CLAIM
FROM INTERRUPTED_CLAIM
WHERE KEYER_ID=#{keyer_id}
THe error is
"A STATEMENT STRING TO BE PREPARED CONTAINS AN INVALID USE OF PARAMETER MARKERS"
and it is complaining about the date function usage of DAYS(DATE(#{batch_dt_ud})).
Can i not pass an ibatis string parameter to the date function ?.
Thanx.

you can try by defining a parameterMap.
<parameterMap id="ParameterMapName" type="ClassTypeOfHowYouPassParameters">
<parameter property="keyer_id" mode="IN"/>
<parameter property="batch_dt_ud" jdbcType="DATE" mode="IN"/>
</parameterMap>
Your query will be something like:
<select id="QueryNameForCalling" parameterMap="ParameterMapName" resultType="string">
SELECT PATIENT_SUFFIX_NM,
INTRPT_CLM_TXT,
DAYS(DATE(#{batch_dt_ud})) - DAYS(INTRPT_CLM_ENTR_TS) AGE_OF_CLAIM
FROM INTERRUPTED_CLAIM
WHERE KEYER_ID=#{keyer_id}
</select>
I am making an assumption that you return a string resultType="string"

Related

Can worksheet filters only be used in WHERE or GROUP BY Clause in Snow?flake

I would like to use the value selected in a filter as a returned column.
For instance like
SELECT :MyFilter;
but I get the following error:
No valid expression found for :Subscription. Expecting "<expression> = :MyFilter" (line 1)
You may need to declare a local variable, assign it the value from :MyFilter, and then use in your query. See the following reference: https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#working-with-variables

How to add a dynamic parameter in Mule4 Select Query

I have a SELECT query for which the entire WHERE condition is coming from a Java class. How can I use the entire WHERE condition in the SELECT query? This is what I tried:
SELECT * FROM EMPLOYEE_TABLE WHERE 1=1 #[payload]
Here payload is a WHERE clause coming from a Java class: NAME = VKP AND STATE = PA AND CITY = KOP
I am getting an error message like " parameter null was not bound for query select…", but I am able to see the payload value coming out from the Java class and in the logs.
You should not use an expression directly in the query. Instead assign the query in an expression to a variable, then use the variable as the query.
For example something like:
<set-variable value="#['SELECT * FROM EMPLOYEE_TABLE WHERE 1=1 ' ++ payload]" name="query" />
<db:select ...>
<db:sql>#[vars.query]</db:sql>
</db:select>
Note that you risk having SQL injections vulnerabilities doing this.

Create current date in String format and parse to date as string in Apex

Goal:
I need to first create a String representing the current date.
Afterwards this String needs to be parsed and used to build an instance of the Date class.
Initial attempt:
In my test class I create a current date as a String input for my tested method in the following manner:
String inputDate = date.today().format(); // 13:28:15:378 USER_DEBUG [24]|DEBUG|17.3.2017
However, when I attempt to create an instance of a Date object like this:
Date dateFromInput = date.valueOf(inputDate);
I receive the following error:
13:28:15:398 FATAL_ERROR System.TypeException: Invalid date: 17.3.2017
Following code
((DateTime)Dob).format('YYYY-MM-dd')
Just Works
Date.format() will return string in current local date format of logged in user.
Date.valueOf needs input string in format yyyy-MM-dd HH:mm:ss in the local time zone.
Below should work:
String inputDate = date.today().format('**yyyy-MM-dd HH:mm:ss**');
Date dateFromInput = date.parse(inputDate);
In the documentation, there is a difference between the parse and valueOf Date methods that escaped me:
parse(stringDate)
Constructs a Date from a String. The format of the String depends on the local date format.
valueOf(stringDate)
Returns a Date that contains the value of the specified String.
What I wanted was the parse:
String inputDate = date.today().format(); /
Date dateFromInput = date.parse(inputDate);
You can try Moment.apex. Here is the link
Datetime dt = new Moment('2018/01/12 10:00:00', 'yyyy/MM/dd HH:mm:ss').toDatetime();

How can I execute a Stored Procedure with DateTime SQL Server values in LINQPad?

I want to see what values a Stored Procedure returns in LINQPad. It takes four args, two strings followed by two DateTimes.
I tried this:
sp_Platypus("Gramps", "WilfredOwen", "2015-09-01 15:16:16.622", "2015-09-30 15:16:16.622");
...but it fails with, "cannot convert from 'string' to 'System.DateTime?'"
This works, but (reasonably) gives me no results:
sp_Platypus("Gramps", "WilfredOwen", DateTime.Now, DateTime.Now);
So I tried to do it with date math this way:
sp_Platypus("Gramps", "WilfredOwen", DateTime.Now-120, DateTime.Now-30);
...but that is rejected because it thinks the last args are not Date vals but int vals
I also tried this (just the date, no time element):
sp_Platypus("Gramps", "WilfredOwen", "2015-09-01", "2015-09-30");
...to no avail, and probably a few other things.
UPDATE
Thanks to JamieD77: If I do this:
sp_Platypus("Gramps", "WilfredOwen", DateTime.Now.AddDays(-120), DateTime.Now.AddDays(-30));
...with "C# Statement(s)" selected from the Language dropdown, and my SQL Server connection selected from the Connection dropdown, it is accepted as valid.
This (using YYYYMMDD format) works:
Select * FROM PlatypusDetail WHERE MemberName = 'Duckbill Bill' AND PoisonToeDate > '20150601' AND PoisonToeDate < '20150630'

MyBatis: "Compile-time" mapper params replacement, keeping prepared stmt but making table names configurable?

MyBatis 3.1.1 allows either #{...} for prepared statements params,
or ${...} for every-time replacement.
I am missing something what would allow to parametrize parts of the SQL statement but still keep it prepared statement; i.e. replace during configuration.
How can I do that? Maybe using some SQL fragments?
Update:
I found:
<sql id="userColumns"> id,username,password </sql>
<select id="selectUsers" parameterType="int" resultType="hashmap">
SELECT <include refid="userColumns"/> some_table WHERE id = #{id}
</select>
See http://www.mybatis.org/core/sqlmap-xml.html#sql
This would be it if ${...} could be used inside of it.
I think I found it...
<sql id="userColumns"> id,username,password </sql>
And then
<select id="selectUsers" parameterType="int" resultType="hashmap">
SELECT <include refid="userColumns"/>
FROM some_table
WHERE id = #{id}
</select>
So I will use ${...} in that and that should get me there.
See http://www.mybatis.org/core/sqlmap-xml.html#sql
in my project we have a very simple solution for this case.
We have a String called TABLENAME in the Data Objects.
When we construct the Objects we initialize the tablename.
and in the sqls we have the tablename enquoutet.
in the DataObject:
String TABLENAME;
public String getTABLENAME() {return TABLENAME;}
public void setTABLENAME(String tablename) {this.TABLENAME = TABLENAME;}
in the sqls:
<delete id="simpleDelete" parameterClass="Integer">
delete from ${jdbc.schema}.$TABLENAME$
WHERE ID = #ID#
</delete>
I don't know if this is the best solution, but it works quite well. I am open for better solutions.
Since <sql> doesn't work as needed for this purpose, I filled a feature request.
http://code.google.com/p/mybatis/issues/detail?id=627

Resources