Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 DateTime not working properly

Author  Topic 

yatesy87
Starting Member

8 Posts

Posted - 2006-03-06 : 05:11:36
Hi I have come across a problem which I cant seem to resolve so hopefully one of you guys will be able to help me.

My database has a field for date_entered and is a DateTime field. My script pulls in the current date and formats it to dd/mm/yyyy. Now todays date is 06/03/2006, I do a response write for DATE() in my browse and it shows up as 06/03/2006 but when it goes into my database it shows up as 03/06/2006. Now if I was to put 13 where 06 is it will then turn into 13/03/2006.

Does anybody know why it does this and what I can do to fix it, below is my script.

<%
session.lcid=2057
' Declaring variables
Dim prefix, lastname, phone, dem, date_entered, data_source, con, sql_insert

' A Function to check if some field entered by user is empty
Function ChkString(string)
If string = "" Then string = " "
ChkString = Replace(string, "'", "''")
End Function

' Receiving values from Form
prefix = ChkString(Request.Form("prefix"))
lastname = ChkString(Request.Form("lastname"))
phone = ChkString(Request.Form("phone"))
dem = ChkString(Request.Form("dem"))
'date_entered = ChkString(Request.Form("date_entered"))
'strDateTime = formatDatetime("%d, %m, %y", Date())

Response.Write(date())

'Create the data source
data_source = "DSN=***;UID=***;PWD=***;DATABASE=***"
'SQL statement to insert values into the designated colums
sql_insert = "insert into contact (prefix, lastname, phone, dem, date_entered) values ('" & _
prefix & "', '" & lastname & "', '" & phone & "', '" & dem & "', '" & date() & "')"

' Creating Connection Object and opening the database
Set con = Server.CreateObject("ADODB.Connection")
con.Open data_source
con.Execute sql_insert

' Done. Close the connection
con.Close
Set con = Nothing
%>

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-03-06 : 05:27:32
Hi,
To avoid Any errors always use yyyy/MM/dd Date format.
So In your Insert Stament Before Inserting in database change the format in "yyyy/MM/dd"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-03-06 : 06:17:13
Seems that the client date format is dmy and server's is mdy. You should format it in your application to be as suggested or use now()

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

yatesy87
Starting Member

8 Posts

Posted - 2006-03-06 : 06:29:33
I have resolved this issue.

What I have done is put the default value for the data type as getdate() and that seems to be working fine for me
Go to Top of Page
   

- Advertisement -