Database Connector Date as Variable

0
Hi all,   I am connecting to a remote database using the database connector. I have connection with this database, the problem is that I want to query on this database. I want to execute a stored procedure that uses 3 variables. The variables are 2 string values and a date. This date is the problem.  The format of the dates in the SQL Server database I am trying to connect is yyyy-MM-dd hh:mm:ss:SSS (with the time normally not being set so typically 0). When I wish to execute a query that activates the stored procedure with the 3 variables I send out the following 'string' in the parameter. 'EXEC dbo.sp_StoredProcedure @dateStart='' & FORMAT(' + formatDateTime($Planner/Date, 'yyyy-MM-dd') + ' "yyyy-mm-dd") & '', @customerName='' & '+$Planner/Customer+' & '', @siteName='' & '+$Planner/Site+' & '' '   This leads to the following strack trace: com.mendix.core.CoreException: com.mendix.core.CoreRuntimeException: com.mendix.systemwideinterfaces.MendixRuntimeException: com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type varchar to date. at com.mendix.basis.component.InternalCore.execute(InternalCore.java:577) Caused by: com.mendix.core.CoreRuntimeException: com.mendix.systemwideinterfaces.MendixRuntimeException: com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type varchar to date. at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:195) Caused by: com.mendix.systemwideinterfaces.MendixRuntimeException: com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type varchar to date. at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:37) Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Error converting data type varchar to date. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:440) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:328) at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) at databaseconnector.impl.JdbcConnector.executeStatement(JdbcConnector.java:94) at databaseconnector.actions.ExecuteStatement.executeAction(ExecuteStatement.java:71) at databaseconnector.actions.ExecuteStatement.executeAction(ExecuteStatement.java:51) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:50) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:76) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:55) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:51) at com.mendix.basis.actionmanagement.ActionManager$1.execute(ActionManager.java:186) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:33) at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:191) at com.mendix.basis.component.InternalCore.execute(InternalCore.java:573) at com.mendix.modules.microflowengine.actions.actioncall.JavaAction.execute(JavaAction.scala:60) at com.mendix.modules.microflowengine.microflow.impl.MicroflowObject.execute(MicroflowObject.java:47) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAfterBreakingIfNecessary(MicroflowImpl.java:200) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAction(MicroflowImpl.java:157) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:50) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:76) at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:55) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:51) at com.mendix.basis.actionmanagement.ActionManager$1.execute(ActionManager.java:186) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:33) at com.mendix.basis.actionmanagement.ActionManager.executeSync(ActionManager.java:191) at com.mendix.basis.component.InternalCore.execute(InternalCore.java:573) at com.mendix.webui.actions.client.ExecuteAction.execute(ExecuteAction.java:135) at com.mendix.webui.requesthandling.ClientRequestHandler$$anonfun$handleRequest$1.apply$mcV$sp(ClientRequestHandler.scala:328) at com.mendix.webui.requesthandling.ClientRequestHandler$$anonfun$handleRequest$1.apply(ClientRequestHandler.scala:313) at com.mendix.webui.requesthandling.ClientRequestHandler$$anonfun$handleRequest$1.apply(ClientRequestHandler.scala:313) at com.mendix.basis.actionmanagement.IMonitoredAction$$anon$1.execute(IMonitoredAction.scala:47) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:33) at com.mendix.basis.actionmanagement.IMonitoredAction$class.monitor(IMonitoredAction.scala:49) at com.mendix.webui.requesthandling.ClientRequestHandler$ClientMonitoredAction.monitor(ClientRequestHandler.scala:431) at com.mendix.webui.requesthandling.ClientRequestHandler.handleRequest(ClientRequestHandler.scala:313) at com.mendix.webui.requesthandling.ClientRequestHandler.handleActionWithSessionRequired(ClientRequestHandler.scala:251) at com.mendix.webui.requesthandling.ClientRequestHandler.handleAction(ClientRequestHandler.scala:218) at com.mendix.webui.requesthandling.ClientRequestHandler.liftedTree1$1(ClientRequestHandler.scala:104) at com.mendix.webui.requesthandling.ClientRequestHandler.processRequest(ClientRequestHandler.scala:96) at com.mendix.externalinterface.connector.RequestHandler.doProcessRequest(RequestHandler.java:40) at com.mendix.external.connector.MxRuntimeConnector$1.execute(MxRuntimeConnector.java:70) at com.mendix.external.connector.MxRuntimeConnector$1.execute(MxRuntimeConnector.java:67) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:33) at com.mendix.external.connector.MxRuntimeConnector.processRequest(MxRuntimeConnector.java:73) at com.mendix.basis.impl.MxRuntimeImpl.processRequest(MxRuntimeImpl.java:858) at com.mendix.m2ee.appcontainer.server.handler.RuntimeHandler.handle(RuntimeHandler.java:41) at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:368) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489) at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:953) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1014) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:861) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:748)   Hence what I think is the problem is that because I format the date it comes in as a string and not as a date. And the database cannot convert this. However if I do not format the date I cannot run the app because the entire query should be a string:  How can I give the date as a variable that can be picked up as a date by the database in this string?
asked
3 answers
1

The correct answer was I first needed to declare the variable as a date for the sql statement:

'DECLARE @date date
set @date = cast('''+formatDateTime($Planner/Date, 'yyyyMMdd') + ''' as date)

EXECUTE dbo.sp_StoredProcedure 
@date  ,
@customerName ='''+$Planner/Customer+''',
@siteName ='''+$Planner/Site+''';
'



 

answered
1

if $DateUpdated != empty

then 'UPDATE Entity1 SET Date1= Date_Updated='''+formatDateTime($Date_Updated, 'yyyy-MM-dd')+'''

else ….

Would that do the trick?

 

answered
0

You can convert your string to a date: https://stackoverflow.com/questions/207190/sql-server-string-to-date-conversion?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

 

Probably something like:

'EXEC dbo.sp_StoredProcedure 
@dateStart='' &  PARSE(' + formatDateTime($Planner/Date, 'yyyy-MM-dd') + ' as date) & '',
@customerName='' & '+$Planner/Customer+' & '',
@siteName='' & '+$Planner/Site+' & ''
'

 

answered