# Insert Date in to SQL Server 2008 Table

**URL:** <https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377>\
**Category:** Q&A\
**Created:** [24 November 2014 12:51 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377 "2014-11-24T12:51:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmyg](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jimmyg](https://community.ofelia.com/u/jimmyg)\
**Post date:** [24 November 2014 12:51 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377/1 "2014-11-24T12:51:10Z")

</div>

Hey folks,

I have a variable in a process. It is a date. It is used to select a date of birth.

This needs to save to a sql server database table, with outher variables, to a field which is a datetime type in the database table.

Now, the sql query, without the date field - works fine. All data goes in.  
With the date field, the sql query fails.

Do I need to do something to the variable ‘date’ for it to work with the sql field ‘datetime’?

Many thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![Sean\_McP](https://dub1.discourse-cdn.com/flex013/user_avatar/community.ofelia.com/sean_mcp/32/932_2.png) [@Sean\_McP](https://community.ofelia.com/u/Sean_McP)\
**Post date:** [25 November 2014 07:51 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377/2 "2014-11-25T07:51:53Z")

</div>

Hi Jim,

I suspect you just need to format the date/time into the correct field format…

‘2013-10-10’ for example…

see also: [here](http://stackoverflow.com/questions/5287427/how-to-insert-datetime-into-the-sql-database-table) and [here.](http://www.codeproject.com/Questions/674060/how-to-insert-date-in-sql-server)

regards  
Seán

---

<div class="post-metadata">

**Author:** ![luqman.subki](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@luqman.subki](https://community.ofelia.com/u/luqman.subki)\
**Post date:** [9 December 2014 04:52 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377/3 "2014-12-09T04:52:45Z")

</div>

Hi,

The initial date format captures in Bonita is “Sun Sep 01 00:00:00 SGT 2013”. In database, i stored it in a column with string data type. Next time i want to use this data, i called the data and hold it in string variable. Then i parse it using this script:

import java.util.Date;  
import java.text.DateFormat;  
import java.text.SimpleDateFormat;

SimpleDateFormat formatter = new SimpleDateFormat(“EEEE MMM dd HH:mm:ss z yyyy”);  
String dateInString = d1;//d1 is a (string) bonita variable which i fetch from database

try {

```
	   Date date = formatter.parse(dateInString);
	   date

   } catch (Exception e)
   {
   System.out.println("Error");
   }

```

This maybe a long way but it might be a last resort =)

---

<div class="post-metadata">

**Author:** ![jimmyg](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jimmyg](https://community.ofelia.com/u/jimmyg)\
**Post date:** [25 November 2014 09:45 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377/4 "2014-11-25T09:45:41Z")

</div>

Hi Sean,  
This worked for me:

def dateString = dateOfBirth.format(“YYYY-MM-dd”)  
return dateString

Once done, I can use the var in my sql query and it saves.

Do you know of a better way? Perhaps a date variable that is automatically the above format?

Thanks,  
Jim

---

<div class="post-metadata">

**Author:** ![Sean\_McP](https://dub1.discourse-cdn.com/flex013/user_avatar/community.ofelia.com/sean_mcp/32/932_2.png) [@Sean\_McP](https://community.ofelia.com/u/Sean_McP)\
**Post date:** [25 November 2014 10:42 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377/5 "2014-11-25T10:42:09Z")

</div>

> Do you know of a better way?

The best I can get is  
`
return dateOfBirth.format(“YYYY-MM-dd”)
`

I think that’s the best you’ll get.

---

<div class="post-metadata">

**Author:** ![jimmyg](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jimmyg](https://community.ofelia.com/u/jimmyg)\
**Post date:** [25 November 2014 11:09 UTC](https://community.ofelia.com/t/insert-date-in-to-sql-server-2008-table/5377/6 "2014-11-25T11:09:00Z")

</div>

Would you do this on submit of a form?

So the output to a variable would be field\_birthDate.format(“YYYY-MM-dd”)

I am currently using a step just to convert the data called ‘convert date’ which runs a groovy connector to do the above.

Doing directly from a form would be much better, what do you think?
