473,473 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problem with date

Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set to
French (Belgium).
Asp.net and Sql server take the short date format of the regional settings
(e.g. 2/08/2007 or 13/08/2007).
I checked both: that's ok.

When i try to insert a date in a datetime field in sql server which is e.g.
13/08/2007, i get the error:
"The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value."

This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" & dbeg &
"','" & dend & "')"

I checked just before inserting the values (with response.write) and they
are: 2/08/2007 and 13/08/2007.

Why doesn't sql server accept those values? They are conform the settings,
no?
Or maybe the Insert command transforms the format of tye date?

Thanks for help
Mark
Aug 2 '07 #1
6 1866
First, Profiler is your friend. Use it to find out that SQL is actually submitted by you app.Never
trust a tool/dev environment, which can do anything it like with a datetime value before presenting
it in a human readable format for you.

Here's a backgrounder on datetime that might help:
http://www.karaszi.com/SQLServer/info_datetime.asp

Also, I strongly encourage you to keep datetime values as date datatypes in your host language, and
use parameterized queries instead of constructing datetime literal. this way, it will always work.
And you also gets tons of other benefits from using parameterized queries.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Mark" <ma**@fit.pmwrote in message news:Ow**************@TK2MSFTNGP02.phx.gbl...
Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set to French (Belgium).
Asp.net and Sql server take the short date format of the regional settings (e.g. 2/08/2007 or
13/08/2007).
I checked both: that's ok.

When i try to insert a date in a datetime field in sql server which is e.g. 13/08/2007, i get the
error:
"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime
value."

This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" & dbeg & "','" & dend & "')"

I checked just before inserting the values (with response.write) and they are: 2/08/2007 and
13/08/2007.

Why doesn't sql server accept those values? They are conform the settings, no?
Or maybe the Insert command transforms the format of tye date?

Thanks for help
Mark
Aug 2 '07 #2
"Mark" <ma**@fit.pmwrote in message
news:Ow**************@TK2MSFTNGP02.phx.gbl...
Why doesn't sql server accept those values?
Because there is no 13th month...
They are conform the settings, no?
No.
Or maybe the Insert command transforms the format of tye date?
It doesn't, unless you parameterise it.

Replace dbeg with dbeg.ToString("dd MMM yyyy") - you may have to cast dbeg
to a DateTime first...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 2 '07 #3
You are passing these dates in as strings. You really should build
parameters for the statement or better yet use stored procedures. But in any
case what is the dateformat set to for that connection? See SET DATEFORMAT
in BooksOnLine for more details. Since you are passing it as a string SQL
Server will try to convert it to a Datetime but it needs to know which is
the month, day, year etc. The preferred way to deal with Dates as strings
is to use the ISO or ANSI formats so there is never a mistake in this
regard. For instance the date in this format will always work regardless of
language or date settings. 'yyyymmdd' See here for more details as
well:

http://www.karaszi.com/SQLServer/info_datetime.asp
Guide to Datetimes
http://www.sqlservercentral.com/colu...qldatetime.asp
Datetimes
http://www.murach.com/books/sqls/article.htm
Datetime Searching

--
Andrew J. Kelly SQL MVP

"Mark" <ma**@fit.pmwrote in message
news:Ow**************@TK2MSFTNGP02.phx.gbl...
Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set to
French (Belgium).
Asp.net and Sql server take the short date format of the regional settings
(e.g. 2/08/2007 or 13/08/2007).
I checked both: that's ok.

When i try to insert a date in a datetime field in sql server which is
e.g. 13/08/2007, i get the error:
"The conversion of a char data type to a datetime data type resulted in an
out-of-range datetime value."

This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" & dbeg
& "','" & dend & "')"

I checked just before inserting the values (with response.write) and they
are: 2/08/2007 and 13/08/2007.

Why doesn't sql server accept those values? They are conform the settings,
no?
Or maybe the Insert command transforms the format of tye date?

Thanks for help
Mark

Aug 2 '07 #4
Hello Mark,
Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set
to
French (Belgium).
Asp.net and Sql server take the short date format of the regional
settings
(e.g. 2/08/2007 or 13/08/2007).
I checked both: that's ok.
When i try to insert a date in a datetime field in sql server which is
e.g.
13/08/2007, i get the error:
"The conversion of a char data type to a datetime data type resulted
in an
out-of-range datetime value."
This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" &
dbeg &
"','" & dend & "')"
I checked just before inserting the values (with response.write) and
they are: 2/08/2007 and 13/08/2007.

Why doesn't sql server accept those values? They are conform the
settings,
no?
Or maybe the Insert command transforms the format of tye date?
Thanks for help
Mark

All your problems will go away if you start using parameters instead of inlining
the date as a string.

DateTime sbeg = new DateTime(2007,2,8);

comd.CommandText = "insert into mytable (datbegin,datend) values(@dstart,
@dend)"
comd.Parameters.Add(new SqlParameter("@dstart", dbeg));
comd.Parameters.Add(new SqlParameter("@dend", dend));

This will make
Aug 2 '07 #5
Hello Mark,
Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set
to
French (Belgium).
Asp.net and Sql server take the short date format of the regional
settings
(e.g. 2/08/2007 or 13/08/2007).
I checked both: that's ok.
When i try to insert a date in a datetime field in sql server which is
e.g.
13/08/2007, i get the error:
"The conversion of a char data type to a datetime data type resulted
in an
out-of-range datetime value."
This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" &
dbeg &
"','" & dend & "')"
I checked just before inserting the values (with response.write) and
they are: 2/08/2007 and 13/08/2007.

Why doesn't sql server accept those values? They are conform the
settings,
no?
Or maybe the Insert command transforms the format of tye date?
Thanks for help
Mark

There are 3 solutions, 2 at the .NET side, 1 at the SQL side

1) All your problems will go away if you start using parameters instead of
inlining the date as a string. This is the best solution you could choose.

DateTime dbeg = new DateTime(2007,2,8);
DateTime dend = new DateTime(2007,2,13);
comd.CommandText = "insert into mytable (datbegin,datend) values(@dstart,
@dend)";
comd.Parameters.AddWithValue("@dstart", dbeg);
comd.Parameters.AddWithValue("@dend", dend);

This will make sure the data is passed as a DateTime.

2) Another option is to format the dates you pass to the query in the following
format: yyyy.mm.dd. You can use string.format to do that easily:

comd.CommandText = string.Format("insert into mytable (datbegin,datend)
values({0:yyyy.MM.dd}, {1:yyyy.MM.dd})", dbeg, dend);

3) You could add a function around your inserted strings to parse the date
in de SQL statement.

comd.CommandText = string.Format("insert into mytable (datbegin,datend)
values(convert(datetime, {0}, 105), convert(datetime, {1}, 105))", dbeg,
dend);

More info on the style id's (105 in this case) can be found here: http://sqljunkies.com/HowTo/6676BEAE...C7FD826E5.scuk


Whichever route you choose consider this:
- have a look at the string.Format function. It's your bets friend when concatenating
lots of strings together in a readable fashion. A
- have a look at parameters for SQL queries. They're faster, easier to read
and maintain and more secure to boot. They also have less trouble with conversions
as you'll find out.

Jesse
Aug 2 '07 #6
Thanks for replying
"Jesse Houwing" <Je***********@nospam-sogeti.nlschreef in bericht
news:33*************************@news.microsoft.co m...
Hello Mark,
>Hi,

i have an application which works with date.
The regional settings of the computer (XP prof. dutch version) are set
to
French (Belgium).
Asp.net and Sql server take the short date format of the regional
settings
(e.g. 2/08/2007 or 13/08/2007).
I checked both: that's ok.
When i try to insert a date in a datetime field in sql server which is
e.g.
13/08/2007, i get the error:
"The conversion of a char data type to a datetime data type resulted
in an
out-of-range datetime value."
This is the code:
comd.CommandText = "insert into mytable (datbegin,datend) values('" &
dbeg &
"','" & dend & "')"
I checked just before inserting the values (with response.write) and
they are: 2/08/2007 and 13/08/2007.

Why doesn't sql server accept those values? They are conform the
settings,
no?
Or maybe the Insert command transforms the format of tye date?
Thanks for help
Mark


There are 3 solutions, 2 at the .NET side, 1 at the SQL side

1) All your problems will go away if you start using parameters instead of
inlining the date as a string. This is the best solution you could choose.

DateTime dbeg = new DateTime(2007,2,8);
DateTime dend = new DateTime(2007,2,13);
comd.CommandText = "insert into mytable (datbegin,datend)
values(@dstart, @dend)";
comd.Parameters.AddWithValue("@dstart", dbeg);
comd.Parameters.AddWithValue("@dend", dend);

This will make sure the data is passed as a DateTime.

2) Another option is to format the dates you pass to the query in the
following format: yyyy.mm.dd. You can use string.format to do that easily:

comd.CommandText = string.Format("insert into mytable (datbegin,datend)
values({0:yyyy.MM.dd}, {1:yyyy.MM.dd})", dbeg, dend);

3) You could add a function around your inserted strings to parse the date
in de SQL statement.

comd.CommandText = string.Format("insert into mytable (datbegin,datend)
values(convert(datetime, {0}, 105), convert(datetime, {1}, 105))", dbeg,
dend);

More info on the style id's (105 in this case) can be found here:
http://sqljunkies.com/HowTo/6676BEAE...C7FD826E5.scuk


Whichever route you choose consider this:
- have a look at the string.Format function. It's your bets friend when
concatenating lots of strings together in a readable fashion. A
- have a look at parameters for SQL queries. They're faster, easier to
read and maintain and more secure to boot. They also have less trouble
with conversions as you'll find out.

Jesse


Aug 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Aing | last post by:
Anyone knows what can be cause of this problem? //////////////////////////////////////////////////////////// typedef struct _date_struct { int date,month,year; }date_struct; Class Date {...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
6
by: jstaggs39 | last post by:
I want to create a Dcount and an If...Then...Else statement to count the number of records in a table based on the date that is entered to run the form. The If....Else statment comes in because if...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
11
by: Geoff Jones | last post by:
Hi I have a table that has a column with Date types. I am trying to view certain rows in the table using a DataView. Using the filter, I can view the rows with, for example, the date equal...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
8
by: sara | last post by:
I have a report that runs fine with data. If there is no data, I have its NO Data event sending a MsgBox and cancelling the report. Then it seems I still get the 2501 message on the Open Report...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
7
by: creative1 | last post by:
Hello everyone. I am experiencing a strange problem that I can't fix on my own. I think I need expert's suggestions for this. The problem is: I want to print account statement (or any other...
11
by: jessy | last post by:
Hi, I have a problem with my DateTimePicker javascript code which i downloaded , the problem is when i pick the date and the date appears in my Text Field and i click Submit the date which i picked...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.