Insert Date into SQL Server

  • Thread starter Thread starter Stefan Richter
  • Start date Start date
S

Stefan Richter

Hi, I am trying to enter data into a table,
but I got problems with my sql statement.

It's something like that:

Dim now As String = DateTime.Now.ToString
INSERT INTO table (date) VALUES(now)

I know there is also ToShortDateString but it didn't change my String in
anyway.

Atm the date in the SQL String looks like: 20.5.2004 09:00:00
and it should like like: 20-May-2004 09:00

Any idea how that is to be done???

Thx,

Stefan
 
Hi

You need to convert to YYYY-MM-DD HH:MM:SS before you can
insert into ms sql server 2000.

Kind Regards
Jorge
 
* "Stefan Richter said:
Hi, I am trying to enter data into a table,
but I got problems with my sql statement.

It's something like that:

Dim now As String = DateTime.Now.ToString
INSERT INTO table (date) VALUES(now)

I know there is also ToShortDateString but it didn't change my String in
anyway.

Atm the date in the SQL String looks like: 20.5.2004 09:00:00
and it should like like: 20-May-2004 09:00

Use an 'InsertCommand' + parameters instead:

Using Parameters with a DataAdapter
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpconusingparameterswithdataadapters.asp>
 
either do this

DateTime.Now.ToString("YYYY-MM-DD") & " " & DateTime.Now.ToString("HH:MM")")
or
Format(Date.Now, "yyyy-mm-dd HH:MM")

you can always use CDate if you are unsure of its proper format

BTW any proper date format will work when trying to insert data into a MSSQL
2000 datetime field. This means

January 1, 2000
January 1, 2000 01:00:00
1/1/2000
1/1/2000 01:00:00
2000-01-01
2000-01-01 01:00:00

are all valid.
 
O' the date structure is setup in the MSSQL settings of your database i.e.
in My server the date would use "/" and ":" as delimiters. if you need the
data in a certain format that is not supported then you will have to use
anther field type or format the output when the data is extracted or alter
the settings of your dB
 
Yeah, that is just what I am trying to do!

I found out that you can use this format in the brackets of the toString
method,

but it never accepts the space between the DD and HH!

DateTime.Now.ToString("YYYY-MM-DD HH:MM")
Stefan
 
To connect to the MS SQL Server I am using ODBC, just in case that makes any
difference!

Thx,

Stefan
 
* "Stefan Richter said:
I found out that you can use this format in the brackets of the toString
method,

but it never accepts the space between the DD and HH!

DateTime.Now.ToString("YYYY-MM-DD HH:MM")

The string doesn't work at all because "DD" is not defined, "MM" stands
for the month, and...

\\\
MsgBox(DateTime.Now.ToString("yyyy-MM-dd HH:mm"))
///
 
Back
Top