# Kdb SQL  (ODBC connexion)

**URL:** <https://forum.kx.com/t/kdb-sql-odbc-connexion/11057>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [June 3, 2016, 11:13am UTC](https://forum.kx.com/t/kdb-sql-odbc-connexion/11057 "2016-06-03T11:13:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![badaouisaad](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@badaouisaad](https://forum.kx.com/u/badaouisaad)\
**Post date:** [June 3, 2016, 11:13am UTC](https://forum.kx.com/t/kdb-sql-odbc-connexion/11057/1 "2016-06-03T11:13:00Z")

</div>

Hi Guys,

I have an sql query on KDB as follows:

sql: "SELECT CUSIP, EXEC\_PRC, **TICK\_TMSTMP** , TICK\_EFF\_DT FROM TABLE WHERE ";

sql,:“TICK\_EFF\_DT = TO\_DATE('”, (string x),“‘,‘YYYY.MM.DD’) &nbsp;AND CUSIP IN (’”, (string z),"') &nbsp;";

The parameters **z and x** in the sql query are cusip and dates resp ( it is a dynamic sql query).&nbsp;

I would like to improve this query by adding another timestamp filter (highlighted in bold)

my timestamp in the database has the following format&nbsp;TICK\_TMSTMP=(‘2016-04-04 06:00:00’).

How can I improve the sql query but adding a timstamp filter please? I would like to keep the same dynamic query but add another dynamic filter that focuses on the timestamp

Many Thanks

S

---

<div class="post-metadata">

**Author:** ![cgervin021](https://avatars.discourse-cdn.com/v4/letter/c/65b543/32.png) [@cgervin021](https://forum.kx.com/u/cgervin021)\
**Post date:** [June 7, 2016, 4:47pm UTC](https://forum.kx.com/t/kdb-sql-odbc-connexion/11057/2 "2016-06-07T16:47:00Z")

</div>

Hi Saad,

If all you need is to format an input to the corresponding SQL filter, then the following should work.

q)format:{@[x 0;4 7;:;“-”]," “,”:“sv “0”^-2$x[1 2 3]}string`date`hh`mm`ss$  
/or more simply..  
q)format:{{@[x[0];4 7;:;”-“],” “,-4\_x[1]}string`date`time$x}  
q)format .z.p  
“2016-06-07 15:06:13”  
q)sql,:“AND TICK\_TMSTMP=('”,format[.z.p],”') "&nbsp;&nbsp;&nbsp; / replace .z.p with the parameter your function is expecting

Hope this helps.

Thanks,

Connor

---

<div class="post-metadata">

**Author:** ![Rolf1](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Rolf1](https://forum.kx.com/u/Rolf1)\
**Post date:** [June 15, 2016, 3:39pm UTC](https://forum.kx.com/t/kdb-sql-odbc-connexion/11057/3 "2016-06-15T15:39:00Z")

</div>

q){" "0:“dv”$:1#x}.z.p  
“2016-06-15 14:37:43”
