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)
 Date Conversion

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-07-10 : 09:11:56
Garceson writes "Dear friends,

I am struggling to insert a date value in "dd-mm-yyyy" format to SQL server table having datatype as Date/Time.

Regional date setting for Server and Local systems are in dd-mm-yyyy format.


I am using following code

Dim DT As String
DT = Now()
DT = Format(DT, "MM/DD/YYYY")

for the inserting into table using command

ins_comm.CommandText = " INSERT INTO CARD (doe) values("& DT &")"

Procedure will end without any error ,but stored value for the date feild will be garbage value like 1900-01-01.

But if i used mm-dd-yyyy format ,it will get inserted.

What could be the reason,How I can save value in dd-mm-yyyy or dd-mm-yy format

Please help!!!


Graceson Mathew"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-07-10 : 09:20:46
Always format the date with YYYYMMDD format which is universal and will work regardless of any date settings

Try

Dim DT As String
DT = Now()
DT = Format(DT, "YYYYMMDD")

ins_comm.CommandText = " INSERT INTO CARD (doe) values('"& DT &"')"

Also, use stored procedures with parameters and avoid using Concatenated SQL statements


Madhivanan

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

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-07-10 : 09:32:44
Use parameters, even if you don't want to use stored procs. And always use proper datatypes, both for your client code and when using SQL Server.

If you are using .Net, then use Date property of a DateTime to return the date part only. And then set a parameter to that value.

Dim DT As StringDateTime
DT = Now().Date
DT = Format(DT, "MM/DD/YYYY")

ins_comm.CommandText = " INSERT INTO CARD (doe) values(@DT)"
ins_comm.parameters.add("@DT",sqlDBType.DateTime).value = DT
ins_comm.executeNonQuery()




- Jeff
Go to Top of Page
   

- Advertisement -