If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > Why does this select generate an error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-21-10, 15:54
Fatbob73 Fatbob73 is offline
Registered User
 
Join Date: Sep 2008
Posts: 13
Why does this select generate an error

Hi everyone!

Why does the following select work...

Code:
select
	count(distinct l.PID)
from
	LocTrackHist l, Serv s
where
	l.SID = s.SID and
	l.PID = s.PID and
	l.currFacID = 'RVH' and
	l.currLocID like 'M08%' and
	datediff(hh, s.startDtm, getdate()) < 24
but this select fails with the error being "Arithmetic overflow during implicit conversion of VARCHAR value '21/06/2010 3:46:58.116 PM' to a DATETIME field ."

Code:
select
	count(distinct l.PID)
from
	LocTrackHist l, Serv s
where
	l.SID = s.SID and
	l.PID = s.PID and
	l.currFacID = 'RVH' and
	l.currLocID like 'M08%' and
	datediff(hh, s.startDtm, '21/06/2010 3:46:58.116 PM') < 24
Reply With Quote
  #2 (permalink)  
Old 06-22-10, 01:14
reddy_546 reddy_546 is offline
Registered User
 
Join Date: May 2010
Location: Hyderabad, India
Posts: 16
USE datediff(hh, s.startDtm, '06/21/2010 3:46PM) < 24

I think it will work fine.

First convert both dates to same format.
__________________
-PavanKumar M Reddy

Last edited by reddy_546; 06-24-10 at 03:02.
Reply With Quote
  #3 (permalink)  
Old 06-22-10, 02:00
agrawal.meet agrawal.meet is offline
Registered User
 
Join Date: Jun 2010
Posts: 51
This could be because of date format. Sybase may be using MM/DD/YYYY format but you are passing date in DD/MM/YYYY format. Since 21 can't be converted to valid month, error is thrown by sybase. Use the solution mentioned by Reddy_546. That should work fine.
Reply With Quote
  #4 (permalink)  
Old 06-22-10, 08:32
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
I also suggest you don't use functions on columns in your where clause
using the datediff function on startDtm means that you won't be able to use an index defined on the startDtm column
instead I suggest
Code:
where startDtm>dateadd(hh,-24,convert(datetime,'21/06/2010 3:46:58.116 PM',103))
Reply With Quote
  #5 (permalink)  
Old 06-22-10, 13:35
Fatbob73 Fatbob73 is offline
Registered User
 
Join Date: Sep 2008
Posts: 13
Thanks for all the suggestions folks!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On