Monday, March 19, 2012

Example of storing a DateTime value in a SQL Server database

Anybody have any examples of how to do this? I just want to input the current date in a database.

Thanks in advancetwo options

1) Set a default value in the table (in design view you set Getdate() in the default field)

2) In an INSERT query


INSERT INTO MyTable (theField) VALUES (Getdate())
|||I know, I wasn't too descriptive. Thank you for your reply. I didn't know about adding
getdate() as a default value.

Actually, what I have is a new user registration form and when they submit their information I want to store it in a SQL Server database. One of the fields in the database is register_date and the format is DateTime.

Using VB.NET in an .aspx page, I don't know how to format the date to store it in the database.

I was extracting the Year, Month, and Day from a DateTime object and then concatenating them into a string to form a new date.

Example:

Public Function formatCurrentDate()
Dim curdate As DateTime
curdate = DateTime.Now
Dim day As Integer = curdate.Day
Dim month As Integer = curdate.Month
Dim dtmformattedDate As DateTime
Dim strDate As String

If day < 10
day = 0 & day
End If

If month < 10
month = 0 & month
End If

strDate = month & day & curdate.Year

dtmformattedDate = Convert.ToDateTime(strDate)

Return dtmformattedDate
End Function

This does not seem to work. I'm really new to all of this so if I'm in the wrong forum please let me know.

Thanks in advance,

CrunkDaddy|||The way a date should be formated is Month/Date/Year. If you do Date/Month/Year, I think you'll need to change a setting in SQL Server (a collation, or something like that).

Do they have to specify Register Date? If its just the date that they registered, when you send the SQL Query over, use Now.Date for the value. VB will send along the current date.


Public Function formatCurrentDate()
Dim curdate As DateTime
curdate = DateTime.Now
Dim day As Integer = curdate.Day
Dim month As Integer = curdate.Month
Dim dtmformattedDate As DateTime
Dim strDate As String

If day < 10
day = 0 & day
End If

If month < 10
month = 0 & month
End If

strDate = month & day & curdate.Year

dtmformattedDate = Convert.ToDateTime(strDate)

Return dtmformattedDate
End Function

|||Really, to avoid collation problems between the webserver and the database server I tink it's better to just use GetDate()

When you live in the US you don't have those problems, I know, but if you live outside the US working with dates is really a PITA (e.g. English OS on the webserver and Dutch OS on the database server, nice...)|||CrunkDaddy, it's wasn't my intention to offend you with my 'Huh?' reply, sorry if I did. :-)|||Not offended at all. Thanks for all your help. I'll let you guys know if I get this to work.|||Hi CrunkDaddy,

The solution is very easy. To avoid any local specific formatting. Just use the ANSI format 'yyyy-mm-dd'. For instance, if you want to store July 24, 2002 in your datetime column:

INSERT INTO myTable (myDateCol) VALUES ('2002-07-24')

The value '2002-07-24' is a string. Now what you have to do in your VB or C# prg is to parse the DateTime value, excerpt the year, month and date to build this string. The ANSI format is insensitive to regional setting or specific collation.

HTH|||When inserting from a web form use this method

If u expect the user will enter the date in the format dmy
set dateformat dmy
insert into table(date_col) values (15/3/2004)

If u expect the user will enter the date in the format mdy
set dateformat mdy
insert into table(date_col) values (3/15/2004)

No comments:

Post a Comment