Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Date in database

Reply
Thread Tools

Date in database

 
 
fniles
Guest
Posts: n/a
 
      01-08-2008
On my machine in the office I change the computer setting to English (UK) so
the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
This problem happens in either Access or SQL Server.
In the database I have a table with Date/time column. The database is
located on a machine that is set to dd/mm/yyyy also.
When I enter date 7/1/08 (as in January 7, 200, it stores it in the
database as 1/7/08 instead of 7/1/08. Why is it like that and how can I make
the database stores it as 7/1/08 ?
Thank you.


 
Reply With Quote
 
 
 
 
Saga
Guest
Posts: n/a
 
      01-08-2008

You can set the regional date format to anything you like, but when using SQLs to
insert or update data, you must use the yyyy-mm-dd format for SQL Server or the
mm/dd/yyyy format for Access.

If using the Addnew method then assigning the date value to the field, you must use
a date variable type:

dim strDate as string

strDate = date

open recordset "rs"

rs.addnew
rs!DateField = strDate
rs.update

The above wont work!!! (for any date format other than mm/dd/yyyy)

Change it to this:

dim dteDate as date

dteDate = date

open recordset "rs"

rs.addnew
rs!DateField = dteDate
rs.update

This example is so trivial, that you can assign Date directly, but when using
calculated dates the variable will most likely be needed.

To build an SQL with a date:

sSQL = "insert into MyTable (Mydate) values ("
sSQL = sSQL & "'" & format$(date, "yyyy-mm-dd") & "')"

For Access change that to:

sSQL = sSQL & "'" & format$(date, "mm/dd/yyyy") & "')"

HTH Saga
--



"fniles" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> On my machine in the office I change the computer setting to English (UK) so the date format is
> dd/mm/yyyy instead of mm/dd/yyyy for US.
> This problem happens in either Access or SQL Server.
> In the database I have a table with Date/time column. The database is located on a machine that is
> set to dd/mm/yyyy also.
> When I enter date 7/1/08 (as in January 7, 200, it stores it in the database as 1/7/08 instead
> of 7/1/08. Why is it like that and how can I make the database stores it as 7/1/08 ?
> Thank you.
>
>



 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      01-08-2008
Access will work fine with yyyy-mm-dd as well. However, you need to delimit
the dates with # characters in Access, not single quotes.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Saga" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> This example is so trivial, that you can assign Date directly, but when
> using
> calculated dates the variable will most likely be needed.
>
> To build an SQL with a date:
>
> sSQL = "insert into MyTable (Mydate) values ("
> sSQL = sSQL & "'" & format$(date, "yyyy-mm-dd") & "')"
>
> For Access change that to:
>
> sSQL = sSQL & "'" & format$(date, "mm/dd/yyyy") & "')"
>
>
> "fniles" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> On my machine in the office I change the computer setting to English (UK)
>> so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
>> This problem happens in either Access or SQL Server.
>> In the database I have a table with Date/time column. The database is
>> located on a machine that is set to dd/mm/yyyy also.
>> When I enter date 7/1/08 (as in January 7, 200, it stores it in the
>> database as 1/7/08 instead of 7/1/08. Why is it like that and how can I
>> make the database stores it as 7/1/08 ?
>> Thank you.



 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      01-08-2008
Thanks for catching that I neglected to swap out the quotes for the # char.

Noted on the yyyy-mm-dd format for Access.

Saga
--


"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
news:(E-Mail Removed)...
> Access will work fine with yyyy-mm-dd as well. However, you need to delimit the dates with #
> characters in Access, not single quotes.
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Saga" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> This example is so trivial, that you can assign Date directly, but when using
>> calculated dates the variable will most likely be needed.
>>
>> To build an SQL with a date:
>>
>> sSQL = "insert into MyTable (Mydate) values ("
>> sSQL = sSQL & "'" & format$(date, "yyyy-mm-dd") & "')"
>>
>> For Access change that to:
>>
>> sSQL = sSQL & "'" & format$(date, "mm/dd/yyyy") & "')"
>>
>>
>> "fniles" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> On my machine in the office I change the computer setting to English (UK) so the date format is
>>> dd/mm/yyyy instead of mm/dd/yyyy for US.
>>> This problem happens in either Access or SQL Server.
>>> In the database I have a table with Date/time column. The database is located on a machine that
>>> is set to dd/mm/yyyy also.
>>> When I enter date 7/1/08 (as in January 7, 200, it stores it in the database as 1/7/08 instead
>>> of 7/1/08. Why is it like that and how can I make the database stores it as 7/1/08 ?
>>> Thank you.

>
>



 
Reply With Quote
 
MikeD
Guest
Posts: n/a
 
      01-08-2008

"fniles" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> On my machine in the office I change the computer setting to English (UK)
> so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
> This problem happens in either Access or SQL Server.
> In the database I have a table with Date/time column. The database is
> located on a machine that is set to dd/mm/yyyy also.
> When I enter date 7/1/08 (as in January 7, 200, it stores it in the
> database as 1/7/08 instead of 7/1/08. Why is it like that and how can I
> make the database stores it as 7/1/08 ?


If the data type of the column is truly one of the various "date" data
types, the format of the date is irrelevant. Don't worry about it.

However, if the data type is actually text or characters, then you've got a
huge problem.


--
Mike
Microsoft MVP Visual Basic


 
Reply With Quote
 
fiefie.niles@gmail.com
Guest
Posts: n/a
 
      01-09-2008
Thank you everybody.
It turns out that in VB it works fine, but it does not work in ASP.
The data type of the column is truly a "date/time" column in Access
and "Datetime" column in SQL Server.

I do need the date to be stored in the correct format in the database,
because in my ASP program I do the following:
sDay = day(d)
sMonth = month(d)
sYear = year(d)
If it is not stored correctly in the database, the above functions do
not return the correct values.

It seems to work when I do the following (it stores 8/1/08 in the
database)
ssql = "update myTBL set colDate = format('8/1/08','dd/mm/yy') where
ID = 1"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = dbConnection
rs.open ssql

But when I do the following, it stores 1/8/08 in the database:
ssql = "select * from myTBL where ID = 1"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = dbConnection
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Source = sSql
rs.Open
rs("colDate") = cdate(#8/1/08#) '--> got the same result when i do
rs("colDate") = cdate("8/1/08")
rs.Update
rs.close
set rs = nothing

I could use the "update" command on the 1st method, but I would like
to use the 2nd method if possible. Is it possible to make the 2nd
method above work ?
Thank you.


On Jan 8, 5:41*pm, "MikeD" <(E-Mail Removed)> wrote:
> "fniles" <(E-Mail Removed)> wrote in message
>
> news:%(E-Mail Removed)...
>
> > On my machine in the office I change the computer setting to English (UK)
> > so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
> > This problem happens in either Access or SQL Server.
> > In the database I have a table with Date/time column. The database is
> > located on a machine that is set to dd/mm/yyyy also.
> > When I enter date 7/1/08 (as in January 7, 200, it stores it in the
> > database as 1/7/08 instead of 7/1/08. Why is it like that and how can I
> > make the database stores it as 7/1/08 ?

>
> If the data type of the column is truly one of the various "date" data
> types, the format of the date is irrelevant. Don't worry about it.
>
> However, if the data type is actually text or characters, then you've got a
> huge problem.
>
> --
> Mike
> Microsoft MVP Visual Basic


 
Reply With Quote
 
SMussler
Guest
Posts: n/a
 
      01-09-2008
>>> I do need the date to be stored in the correct format in the database,

The database has it's own way of storing dates - typically in a Julian
value.
How you want to display it is done via formating.

Take a look at this link:
http://www.sql-server-performance.co...tatype_p1.aspx

I myself work mainly with Oracle, which I think does something similar and
occasionally with Access.

Not sure about Access, but a google search could get you the details I'm
sure.
But what the Database engine itself is storing is irrelevant.
What's important is getting the data entered properly and displayed as
needed.

Steve Mussler

 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      01-09-2008

Inline

Saga

--


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Thank you everybody.
It turns out that in VB it works fine, but it does not work in ASP.
The data type of the column is truly a "date/time" column in Access
and "Datetime" column in SQL Server.

I do need the date to be stored in the correct format in the database,
because in my ASP program I do the following:
sDay = day(d)
sMonth = month(d)
sYear = year(d)
If it is not stored correctly in the database, the above functions do
not return the correct values.

It seems to work when I do the following (it stores 8/1/08 in the
database)
ssql = "update myTBL set colDate = format('8/1/08','dd/mm/yy') where
ID = 1"

***Reply***
That format statement just does not look right. Use 4 digits for year
and yyyy-mm-dd format. Using dd/mm/yy will cause problems.
***

Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = dbConnection
rs.open ssql

But when I do the following, it stores 1/8/08 in the database:
ssql = "select * from myTBL where ID = 1"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = dbConnection
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Source = sSql
rs.Open
rs("colDate") = cdate(#8/1/08#)
'--> got the same result when i do rs("colDate") = cdate("8/1/08")

***Reply***
When specifying a literal date (#8/1/08#) VB always expects mm/dd/yyyy
format. So the above will be interpreted as Aug 1st, 2008. The CDate(##)
statement above is useless since you are in effect converting a date into
a date.

On the other hand, cdate("8/1/08") is converting the string "8/1/08"into a
date which is a step in the right direction, but it is still not 100% "safe":

"CDate recognizes date formats according to the locale setting of your
system." - MSDN Library

I did the following in the immediate window:

? cdate("8/1/08")
08/01/2008 (2nd) 8th of Jan
8/1/2008 (1st) Aug 1st

I set my regional settings to English US for the 1st try. I then set the
regional settings to English UK for the 2nd test. Note how the "same" date
was displayed, but is intepreted differently depending on locale. Best to stay
away from having dates in strings and using these to update tables or do
further date calculations where the month and day could be ambiguous.

I did another test (in the immediate wndow):

? #8/1/2008#
8/1/2008 US setting Aug 1st 2008
01/08/2008 UK setting 1st of Aug 2008 Same date!!!

Note how the above literal is interpreted as the same date but when displayed
it is done using the correct locale format. As mentioned before, when the #
char is used to specify a date literal it is always interpreted as mm/dd/yyyy.
***

rs.Update
rs.close
set rs = nothing

I could use the "update" command on the 1st method, but I would like
to use the 2nd method if possible. Is it possible to make the 2nd
method above work ?
Thank you.


***PS: I have no idea why the OP's text was not indented with the ">"
char. It seems to have been only for this message.



On Jan 8, 5:41 pm, "MikeD" <(E-Mail Removed)> wrote:
> "fniles" <(E-Mail Removed)> wrote in message
>
> news:%(E-Mail Removed)...
>
> > On my machine in the office I change the computer setting to English (UK)
> > so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
> > This problem happens in either Access or SQL Server.
> > In the database I have a table with Date/time column. The database is
> > located on a machine that is set to dd/mm/yyyy also.
> > When I enter date 7/1/08 (as in January 7, 200, it stores it in the
> > database as 1/7/08 instead of 7/1/08. Why is it like that and how can I
> > make the database stores it as 7/1/08 ?

>
> If the data type of the column is truly one of the various "date" data
> types, the format of the date is irrelevant. Don't worry about it.
>
> However, if the data type is actually text or characters, then you've got a
> huge problem.
>
> --
> Mike
> Microsoft MVP Visual Basic



 
Reply With Quote
 
Dooza
Guest
Posts: n/a
 
      01-09-2008
fniles wrote:
> On my machine in the office I change the computer setting to English (UK) so
> the date format is dd/mm/yyyy instead of mm/dd/yyyy for US.
> This problem happens in either Access or SQL Server.
> In the database I have a table with Date/time column. The database is
> located on a machine that is set to dd/mm/yyyy also.
> When I enter date 7/1/08 (as in January 7, 200, it stores it in the
> database as 1/7/08 instead of 7/1/08. Why is it like that and how can I make
> the database stores it as 7/1/08 ?
> Thank you.


Would setting the locale help?
Not sure if your using VBScript, but here is something about it:
http://www.w3schools.com/vbscript/func_setlocale.asp

Steve
 
Reply With Quote
 
Saga
Guest
Posts: n/a
 
      01-09-2008
Although setting the locale via VB6 code -could- be a potential "fix", I humbly
advice against it as it might make more enemies than friends. One's app
should adapt the locale setting, NOT the locale setting to the app.

Saga
--



"Dooza" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> fniles wrote:
>> On my machine in the office I change the computer setting to English (UK) so the date format is
>> dd/mm/yyyy instead of mm/dd/yyyy for US.
>> This problem happens in either Access or SQL Server.
>> In the database I have a table with Date/time column. The database is located on a machine that
>> is set to dd/mm/yyyy also.
>> When I enter date 7/1/08 (as in January 7, 200, it stores it in the database as 1/7/08 instead
>> of 7/1/08. Why is it like that and how can I make the database stores it as 7/1/08 ?
>> Thank you.

>
> Would setting the locale help?
> Not sure if your using VBScript, but here is something about it:
> http://www.w3schools.com/vbscript/func_setlocale.asp
>
> Steve



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments