[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

format date val to insert into sql

Hi, i am using vb.net 2005
I have a date value in a variable declared as date, I am then trying to insert it into sql table. on one my dev machine the value in my SQL insert statement is 2008/06/30, but on my dev server its being formatted to 30/08/2008. I have checked the regional settings on both machines and they are set to dd/MM/yyyy
So my question is, is there away for me then to "force" the date to yyyy/MM/dd in my insert statement so then its in the correct format on my dev server?
My SQL field is defined as a DateTime datatype

thanks
0
CraigLazar
Asked:
CraigLazar
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
Éric MoreauSenior .Net ConsultantCommented:
if your field datatype is DateTime (as you said) then your date are stored correctly.

the format of the date is not forced by the server but by the computer that runs the query. if you want to force the format, you can always do something like this:
select CONVERT(VARCHAR(10),GETDATE(),120)
0
 
shorakCommented:
try format(datefield, "yyyy/MM/dd")

Rob
0
 
ladarlingCommented:
You can use the format command:

Dim stdDate As String = Format(Now, "yyyy/MM/dd")
Note that the format identifier *is* case sensitive. 'MM' is 2 character representation of the month, 'mm' , though, is minutes.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CraigLazarAuthor Commented:
Hi emoreau,
ok i thought as much, however on my dev server the sql statement looks like this when passed to sql
INSERT INTO tblLoadSet (LoadSetID,ManCoID,QED,Processed,FileCount,BatchID)  VALUES ('a1d5bb7e-41ac-4b97-87a0-771cc21f2389','CORO','30/06/2008','No',1,4);

On my dev machine its
INSERT INTO tblLoadSet (LoadSetID,ManCoID,QED,Processed,FileCount,BatchID)  VALUES ('a1d5bb7e-41ac-4b97-87a0-771cc21f2389','CORO','2008/06/30','No',1,4);

So the date is swopped around, so sql generates an error on the first sql statement

to shorak,
I tried that and it seems to have inserted into the table on the dev server, however using this format, will it swop the day and month around with a date like 7th of June to the 6th of July?

thanks allot
0
 
Éric MoreauSenior .Net ConsultantCommented:
then you have to modify your code. what is creating this INSERT statement in your code? this is the place where you have to format the date correctly using YourDateVariable.ToString("yyyy/MM/dd")
0
 
CraigLazarAuthor Commented:
hi,
ok i am building a sql string set to to a string valriable then passing that to a function which then executes the sql statement using the sql command object

i have adjusted it to the following to make sure that the month is in long format and to hopefully make sure that the day and month do not get swopped around
SqlInsert = "INSERT INTO tblLoadSet (LoadSetID,ManCoID,QED,Processed,FileCount,BatchID)  VALUES ('" & LoadSETID_GUID.ToString() & "','" & strData(1) & "','" & Format(CurQEDate, "yyyy/MMMM/dd") & "','No'," & FileCount & "," & strData(2) & ");"
0
 
Éric MoreauSenior .Net ConsultantCommented:
so is it working now?

BTW, you shouldn't use Format(CurQEDate, "yyyy/MMMM/dd") just in case you run your query against a non-english installation of SQL Server or a non-english client.
0
 
shorakCommented:
Hi Craig

The format function should not swap around no matter what maching you use it on because the function is forcing the date out in the format specified.

As to whether this will be affected by how the data is stored in the table, I am going to make a big assumption here since my technical knowledge of SQL server is limited. The date format will probably depend on how the data was entered into the table in the first place. if all the data was entered in yyyy-mm-dd format then it would not allow a user to enter yyyy-dd-mm hence all the source data in the table should be in this fixed format. When using the format function, It will know how the original date is stored in the table and hence know which numbers are the month and which numbers are the day.

But since your using the long month name format then you should be okay.

Rob
0
 
mordizerCommented:
If you're using a Date type then I tend to call the .ToLongDateString method before I pass it into SQL Server as text - that makes it write it out verbose (e.g 02/02/08 would be output as '2 February 2008') and then SQL doesn't get the chance to get it messed up, I'm not sure this is a technique most experts would recommend but unless I'm using a parameter of a datetime type then I find it the most convenient way of doing it.

If you're using a SQLCommand object then you can always do something like this:
Dim dtmDateValue as date="02 February 2008"
Dim oCmd as new sqlcommand("Insert into tblTest (testitem1,aDateColumn) values ('testitem1',@YourDateParameter)")
ocmd.parameters.add("@YourDateParameter",sqldbtype.datetime).value=dtmDateValue
ocmd.executenonquery
If you're using a .net date type and passing it to a SQLCommand date type parameter then I don't think it can get it wrong.
0
 
CraigLazarAuthor Commented:
Hi,
ok managed to sort it out, for Inserting into MSSQL i am using yyyy/MMM/dd and then for Inserting into MySQL u have to drop the 1 m to yyyy/MM/dd other wise MySQL does not store the date at all

thanks allot for the help

cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now