|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 7:12 AM
Points: 89,
Visits: 290
|
|
Using SQL2005, I'm importing call data from my Cisco Communications Manager. The data is a text file, and the date/time fields are in a numerical, integer format (UNIX EPOH format): 1251781074
Cisco told me to convert that to a readable DATETIME format with this formula:
myfield = DATEADD(hour,-5,(dateadd(second ,myfield, '1/1/1970')))
This works correctly, but since the data is stored as an INT field, I cannot put a clause on my results. So I'm trying to do something like this:
DECLARE @StartDateTime DATETIME ,@EndDateTime DATETIME
SELECT @StartDateTime = '10/13/2009 08:00' SELECT @EndDateTime = '10/13/2009 23:00' ...
WHERE DATEADD(hour,-4,(dateadd(second ,myfield, '1/1/1970'))) >= @StartDateTime AND DATEADD(hour,-4,(dateadd(second ,myfield, '1/1/1970'))) <= @EndDateTime
I tried changing the fields to DATETIME but it will not let me, and since the fields are INT, it's ignoring my variables in DATETIME format.
Is there any way to set this field to a DATETIME so that I can set the WHERE clause on it? Or, format the variable into an INT to compare?
I tried setting the variable to: SELECT @StartDateTime = 1251781074 but that won't work either.
Any help is greatly appreciated, thanks!
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
Try this:
declare @StartInt int, @EndInt int;
select @StartInt = datediff(second, dateadd(hour, -4, '1/1/1970'), @StartDateTime), @EndInt = datediff(second, dateadd(hour, -4, '1/1/1970'), @EndDateTime);
Where MyField >= @StartInt and MyField <= @EndInt;
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, August 02, 2011 3:36 AM
Points: 579,
Visits: 1,803
|
|
Sorry if I've not understood ur requirement, but does this work?
drop table t1 Create table t1 (tim int, rem varchar(100)) select 86400 INSERT INTO t1 VALUES (1251781074, 'day1') INSERT INTO t1 VALUES (1251781074 + 86400, 'day2') INSERT INTO t1 VALUES (1251781074 + 2*86400, 'day3') INSERT INTO t1 VALUES (1251781074 + 3*86400, 'day4') INSERT INTO t1 VALUES (1251781074 + 4*86400, 'day5')
Select DATEADD(hour,-4,(dateadd(second ,tim, '1/1/1970'))), * From t1
DECLARE @StartDateTime DATETIME ,@EndDateTime DATETIME SELECT @StartDateTime = '2009-09-02 00:57:54.000' SELECT @EndDateTime = '2009-09-03 00:57:54.000 '
Select * from t1 Where DATEADD(hour,-4,(dateadd(second ,tim, '1/1/1970'))) >= @StartDateTime AND DATEADD(hour,-4,(dateadd(second ,tim, '1/1/1970'))) <= @EndDateTime
---------------------------------------------------------------------------------
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, August 02, 2011 3:36 AM
Points: 579,
Visits: 1,803
|
|
ohh sorry, dint see Gsquared's response!
---------------------------------------------------------------------------------
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
One reason I worked it the other direction is that it gets rid of the functions in the Where clause. That's usually a good idea for performance. Probably doesn't matter in this case, but it's a good habit to be in.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, August 02, 2011 3:36 AM
Points: 579,
Visits: 1,803
|
|
GSquared (10/15/2009) One reason I worked it the other direction is that it gets rid of the functions in the Where clause. That's usually a good idea for performance. Probably doesn't matter in this case, but it's a good habit to be in.
ah yes thats true :) Functions on any column having an index is not suggested if you are looking for better performance
---------------------------------------------------------------------------------
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 7:12 AM
Points: 89,
Visits: 290
|
|
Thanks so much for the help, I really appreciate it. Here's what I've got, but I'm gettig an error on the conversion:
Msg 8115, Level 16, State 2, Line 13 Arithmetic overflow error converting expression to data type datetime.
It's a pretty simple query, hitting a table of call logs, trying to find incoming and outgoing calls for an extension (386). Again, any help is greatly appreciated!
DECLARE @StartDateTime DATETIME ,@EndDateTime DATETIME, @StartInt int, @EndInt int;
SELECT @StartDateTime = '09/01/2009 01:00' SELECT @EndDateTime = '10/13/2009 23:00'
select @StartInt = datediff(second, dateadd(hour, -4, '1/1/1970'), @StartDateTime), @EndInt = datediff(second, dateadd(hour, -4, '1/1/1970'), @EndDateTime);
select DISTINCT callingPartyNumber AS 'Calling Number', originalCalledPartyNumber AS 'Intended Called Number', finalCalledPartyNumber AS 'Final Destination', datetimeorigination = DATEADD(hour,-5,(dateadd(second ,dateTimeOrigination, '1/1/1970'))), datetimedisconnect = DATEADD(hour,-5,(dateadd(second ,dateTimedisconnect, '1/1/1970'))), duration AS 'Duration (seconds)'
from CDR
where finalcalledpartynumber = ('386') OR callingPartyNumber = ('386') AND DATEADD(hour,-4,(dateadd(second ,dateTimeOrigination, '1/1/1970'))) >= @StartInt AND DATEADD(hour,-4,(dateadd(second ,dateTimeOrigination, '1/1/1970'))) <= @EndInt -- AND (duration) < 9
order by datetimedisconnect
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, August 02, 2011 3:36 AM
Points: 579,
Visits: 1,803
|
|
Thats because you are mixing both of our suggestions!
try this in your where clause
where finalcalledpartynumber = ('386') OR callingPartyNumber = ('386') AND dateTimeOrigination >= @StartInt AND dateTimeOrigination<= @EndInt
---------------------------------------------------------------------------------
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:45 PM
Points: 15,442,
Visits: 9,572
|
|
It probably actually needs to be:
where (finalcalledpartynumber = ('386') OR callingPartyNumber = ('386')) AND dateTimeOrigination >= @StartInt AND dateTimeOrigination<= @EndInt Without the parentheses around the party numbers, it won't use the second half of the Where clause correctly.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 7:12 AM
Points: 89,
Visits: 290
|
|
thanks so much, that seems to work!
I had to change it to:
where dateTimeOrigination >= @StartInt AND dateTimeOrigination <= @EndInt AND (finalcalledpartynumber = '386' OR callingPartyNumber = '386')
to get it to work correctly.
Also, in the conversion, is it suposed to be -4 or -5?
select @StartInt = datediff(second, dateadd(hour, -5, '1/1/1970'), @StartDateTime),
|
|
|
|