Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date insert issue into SQL server DateTime field?

Status
Not open for further replies.

gus121

Technical User
May 9, 2002
298
GB
Hi when i run this sql i get this error

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.


The ASP
Code:
("INSERT INTO offer(OfferCode, OfferPercent, OfferStartDate, OfferFinishDate, OfferDescription) VALUES ('" & OfferCode & "'," & OfferPercent & ",'" & CDate(Now) & "','" & CDate(DateAdd("d", DaysActive, Now)) & "','" & offerDescription & "')")

The SQL:
Code:
INSERT INTO offer(OfferCode, OfferPercent, OfferStartDate, OfferFinishDate, OfferDescription) VALUES ('24082005-162928',1,'24/08/2005 16:29:28','04/08/2005 16:29:28','')

-Gus
 
sounds like a problem with the regional settings...check the datetime settings on your server...

also try this...

Format(yourdatefield, "dd-mm-yyyy hh:nn:ss") forcing to following the particular format...

-DNG
 
Hi DNG,

I know i probably been a bit dim but i tried this as i thought Format must be a sQL command:

Code:
INSERT INTO offer(OfferCode, OfferPercent, OfferStartDate, OfferFinishDate, OfferDescription) VALUES ('24082005-174507',1, Format(04/08/2005 17:45:07, dd-mm-yyyy hh:nn:ss), Format(24/08/2005 17:45:07, dd-mm-yyyy hh:nn:ss),'')
[\code]

I got various errors:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '17'.
Server: Msg 132, Level 15, State 1, Line 1
The label 'hh' has already been declared. Label names must be unique within a query batch or stored procedure.
Server: Msg 132, Level 15, State 1, Line 1
The label 'nn' has already been declared. Label names must be unique within a query batch or stored procedure.

-Gus
 
it is an ASP function so resolve the value outside of your sql string

Code:
("INSERT INTO offer(OfferCode, OfferPercent, OfferStartDate, OfferFinishDate, OfferDescription) VALUES ('" & OfferCode & "'," & OfferPercent & ",'" & Format(Now, "dd-mm-yyyy hh:nn:ss")  & "','" & Format(DateAdd("d", DaysActive, Now), "dd-mm-yyyy hh:nn:ss") & "','" & offerDescription & "')")

Though I don't think this will work for you - I think you will need to change the month and the day bits around to be 'Americanised' - SQL server is probably reading the day (e.g. 24) as the month and telling you that there is no such month.. i.e. Out-of-range.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Hi damber,

I have never heared of an inbuilt asp format function!

response.write(Format(Now, "dd-mm-yyyy hh:nn:ss"))

When i run this i get back. Type mismatch: 'Format'

-Gus
 
Oops, sorry - that's a VB function, not VBScript. You can build your own component (very simple) to run the format function in VB and pass it back to VBScript (ASP).

You should be able to change the LCID (Locale Setting) so that it uses the correct date time format for your SQL server.

here's a link:

or you could split the date into bits either as an array or using the Day() Month() year() functions and placing them in the order you prefer.



A smile is worth a thousand kind words. So smile, it's easy! :)
 
Hi thanks damber,

I have looked at the information you have given me. I am aware how to create a vb function. thanks

Think its probably better to write a stored procedure and do the date manipulation bit within the proc therefore no need to change the Local Settings for the whole site!

thanks

-Gus
 
Gus,

You should use Stored Procedures anyway - they are much more efficient, and helps to separate your application layer from the data layer.

You could just create a simple DateForSQL function that uses the DatePart or Day/Month/Year to present back a string that represents the correct format.

Or present the long version of the date (e.g. the full month name in particular) so there is no ambiguity, e.g. "August 25, 2005"

You can use the LCID inside a function and only apply it to the datetime you are working on at that time.

here's a link I found describing the issue quite well:


A smile is worth a thousand kind words. So smile, it's easy! :)
 
Gus,

post the current query that you are trying...and also post what errors are you getting...

-DNG
 
how about using some convert() functions...post back your query...

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top