Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 convert varchar to datetime

Author  Topic 

jn-at-uk
Starting Member

20 Posts

Posted - 2005-02-14 : 06:22:25
Hi
My field AbsTimeStamp is of datatype dateTime in Table1
@startDate and @enddate is being passed as varchar(12). So I assumed i hav 2
convert the datatypes to get results.
This is my sql below but i get the following error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Please assist
------------------------------------------------

Select @Select = 'SELECT Count(*) FROM Table1 WHERE ID =41'
Select @Select = @Select + 'AND (AbsTimeStamp BETWEEN ' cast(@startDate as datetime) & 'AND' & cast(@endDate as datetime) & ')'
exec(@Select)

nr
SQLTeam MVY

12543 Posts

Posted - 2005-02-14 : 06:28:07
You need to look at the format of the date input and use a style on the convert probably - look at convert rather than cast.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

BammBamm
Starting Member

9 Posts

Posted - 2005-02-14 : 14:13:10
I agree... Also consider the "quality" of your data and what it represents, i.e.:

Is it possible to use/expect an open-ended end date (blank/null) instead of a fixed date in your model? If so, pass/re-assign a value for end date.

Do all of your date variables conform to accepted formats? Look at help on cast/convert, convert has multiple date/time formats.

Is there any possibility for bad data being passed?
If so, how can it be handled up front?
Go to Top of Page
   

- Advertisement -