Friday, February 24, 2012

Error trying to save a date field

Hi there, right now i'm working with a VB6 project that connects to a
SQLSERVER 2000.
I discovered a problem that, i hope you can help me.
The problem is that, when i try to save a record with a date field - the
date saved in database is 2 days more that the typed date. ALWAYS 2 DAYS!!!
Don't know why.
In order to save a record to DB i try to use val = cdec(val), where val is a
date value, and for strings values i just use val = "'" & val & "'", and so
on... for diferent types of fields.
Because i'm developing for a spanish environment i decided to convert dates
in the right format ("dd/mm/yyyy") and try to have regional settings that
match national language and location.
If anybody can help me, please i will appreciate it.
Felix.You probably represent the date as some umber datatype (float, int or so) an
d the VB "base date"
differs from the SQL Server "base date" by 2 days. Never use numbers to repr
esent datetime in SQL,
represent them as strings. Or better yet, use a date datatype in your host l
anguage and use
parametized queries. Read here about datetime datatype in SQL Server:
http://www.karaszi.com/SQLServer/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Felix Quevedo C." <FelixQuevedoC@.discussions.microsoft.com> wrote in messag
e
news:3FBFD807-414F-4846-9DCE-A90AAFF1CB95@.microsoft.com...
> Hi there, right now i'm working with a VB6 project that connects to a
> SQLSERVER 2000.
> I discovered a problem that, i hope you can help me.
> The problem is that, when i try to save a record with a date field - the
> date saved in database is 2 days more that the typed date. ALWAYS 2 DAYS!!
!
> Don't know why.
> In order to save a record to DB i try to use val = cdec(val), where val is
a
> date value, and for strings values i just use val = "'" & val & "'", and s
o
> on... for diferent types of fields.
> Because i'm developing for a spanish environment i decided to convert date
s
> in the right format ("dd/mm/yyyy") and try to have regional settings that
> match national language and location.
> If anybody can help me, please i will appreciate it.
> Felix.|||Sounds like VB is converting the date to an integer and back again, and
using its own base date (1899-12-30) instead of SQL Server's (1900-01-01).
Try taking the literal string and passing it as a string. If you do this,
be sure to type in a proper format, e.g. YYYYMMDD. If you type in
mm/dd/yyyy there are several points along the way where this can easily
convert to dd/mm/yyyy.
A
"Felix Quevedo C." <FelixQuevedoC@.discussions.microsoft.com> wrote in
message news:3FBFD807-414F-4846-9DCE-A90AAFF1CB95@.microsoft.com...
> Hi there, right now i'm working with a VB6 project that connects to a
> SQLSERVER 2000.
> I discovered a problem that, i hope you can help me.
> The problem is that, when i try to save a record with a date field - the
> date saved in database is 2 days more that the typed date. ALWAYS 2
> DAYS!!!
> Don't know why.
> In order to save a record to DB i try to use val = cdec(val), where val is
> a
> date value, and for strings values i just use val = "'" & val & "'", and
> so
> on... for diferent types of fields.
> Because i'm developing for a spanish environment i decided to convert
> dates
> in the right format ("dd/mm/yyyy") and try to have regional settings that
> match national language and location.
> If anybody can help me, please i will appreciate it.
> Felix.|||thanks a lot Tibor. My problem is solved - Felix.
"Tibor Karaszi" wrote:

> You probably represent the date as some umber datatype (float, int or so)
and the VB "base date"
> differs from the SQL Server "base date" by 2 days. Never use numbers to re
present datetime in SQL,
> represent them as strings. Or better yet, use a date datatype in your host
language and use
> parametized queries. Read here about datetime datatype in SQL Server:
> http://www.karaszi.com/SQLServer/info_datetime.asp
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Felix Quevedo C." <FelixQuevedoC@.discussions.microsoft.com> wrote in mess
age
> news:3FBFD807-414F-4846-9DCE-A90AAFF1CB95@.microsoft.com...
>

No comments:

Post a Comment