Solved

BETWEEN syntax on INT date fields

Posted on 2011-03-11
14
329 Views
Last Modified: 2012-05-11
Hi

I want to perform a BETWEEN in the code below.

SELECT     TOP (100) PERCENT callingPartyNumber, originalCalledPartyNumber, DATEADD(hh, 2, DATEADD(s, dateTimeOrigination, '1 jan 1970')) AS dato, DATEADD(hh, 2,
                      DATEADD(s, dateTimeConnect, '1 jan 1970')) AS start, DATEADD(hh, 2, DATEADD(s, dateTimeDisconnect, '1 jan 1970')) AS slut, duration
FROM         dbo.cdrdata
WHERE     (dateTimeConnect BETWEEN '2005-01-01' AND '2011-01-01')

I get the error:
Conversion failed when converting the varchar value '2005-01-01' to data type int.

What should I write instead?
0
Comment
Question by:jensjakobsen
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 35108407
Because type of dateTimeConnect  is integer
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35108545
what about this:

SELECT     TOP (100) PERCENT callingPartyNumber, originalCalledPartyNumber, DATEADD(hh, 2, DATEADD(s, dateTimeOrigination, '1 jan 1970')) AS dato, DATEADD(hh, 2, 
                      DATEADD(s, dateTimeConnect, '1 jan 1970')) AS start, DATEADD(hh, 2, DATEADD(s, dateTimeDisconnect, '1 jan 1970')) AS slut, duration
FROM         dbo.cdrdata
WHERE     (dateTimeConnect BETWEEN datediff(day, 0, '2005-01-01' ) AND datediff(day, 0, '2011-01-01')) 

Open in new window


however, storing dates as int's is not really a good idea.

http://www.experts-exchange.com/A_1499.html



0
 
LVL 1

Author Comment

by:jensjakobsen
ID: 35108566
Hi Angel

No errors but no results either. And I know for a fact that there are a lot of results.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35108586
then, run:
select datediff(day, 0, '2005-01-01' ), datediff(day, 0, '2011-01-01') 

Open in new window

which returns: 38351      and 40542

so, what if you run this:
SELECT     TOP (100) PERCENT callingPartyNumber, originalCalledPartyNumber, DATEADD(hh, 2, DATEADD(s, dateTimeOrigination, '1 jan 1970')) AS dato, DATEADD(hh, 2, 
                      DATEADD(s, dateTimeConnect, '1 jan 1970')) AS start, DATEADD(hh, 2, DATEADD(s, dateTimeDisconnect, '1 jan 1970')) AS slut, duration
FROM         dbo.cdrdata
WHERE     (dateTimeConnect BETWEEN 38351	and 40542 ) 

Open in new window

0
 
LVL 14

Expert Comment

by:Daniel_PL
ID: 35108888
Hi, dateTimeConnect is an integer.


SELECT     TOP (100) PERCENT callingPartyNumber, originalCalledPartyNumber, DATEADD(hh, 2, DATEADD(s, dateTimeOrigination, '1 jan 1970')) AS dato, DATEADD(hh, 2,
                      DATEADD(s, dateTimeConnect, '1 jan 1970')) AS start, DATEADD(hh, 2, DATEADD(s, dateTimeDisconnect, '1 jan 1970')) AS slut, duration
FROM         dbo.cdrdata
WHERE     (DATEADD(s,dateTimeConnect,'1 jan 1970') BETWEEN '2005-01-01' AND '2011-01-01')

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 35112430
Can you try this. What is the data type of dateTimeConnect ?
SELECT TOP ( 100 ) PERCENT callingPartyNumber, 
                           originalCalledPartyNumber, 
                           DATEADD(hh,2,DATEADD(s,dateTimeOrigination,'1 jan 1970')) AS dato, 
                           DATEADD(hh,2,DATEADD(s,dateTimeConnect,'1 jan 1970'))     AS start, 
                           DATEADD(hh,2,DATEADD(s,dateTimeDisconnect,'1 jan 1970'))  AS slut, 
                           duration 
  FROM dbo.cdrdata 
 WHERE (dateTimeConnect BETWEEN '20050101' AND '20110101')

Open in new window

0
 
LVL 1

Author Comment

by:jensjakobsen
ID: 35321461
Hi Sharath

This one gave no errors but no results either :(

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 40

Expert Comment

by:Sharath
ID: 35326998
Can you answer this question?
What is the data type of dateTimeConnect ?
0
 
LVL 1

Author Comment

by:jensjakobsen
ID: 35900882
Hi Sharat, I'm sorry for the delayed answer. The datatype for the dateTimeConnect is INTEGER.
0
 
LVL 14

Expert Comment

by:Daniel_PL
ID: 35900984
Did you try my answer from comment ID 35108888 ?
 
SELECT     TOP (100) PERCENT callingPartyNumber, originalCalledPartyNumber, DATEADD(hh, 2, DATEADD(s, dateTimeOrigination, '1 jan 1970')) AS dato, DATEADD(hh, 2,
                      DATEADD(s, dateTimeConnect, '1 jan 1970')) AS start, DATEADD(hh, 2, DATEADD(s, dateTimeDisconnect, '1 jan 1970')) AS slut, duration
FROM         dbo.cdrdata
WHERE     (DATEADD(s,dateTimeConnect,'1 jan 1970') BETWEEN '2005-01-01' AND '2011-01-01')

Open in new window

0
 
LVL 1

Author Comment

by:jensjakobsen
ID: 35999767
Yes I did - and it didn't work :(
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35999775
still waiting for feedback on my comment above.

anyhow, we will need more concrete examples of what should "work" and what should "not work": sample data ...
0
 
LVL 1

Author Comment

by:jensjakobsen
ID: 36430481
Hi all. I'm very sorry for this huge delay.

I received help from IT pros at work and they were able to help me out.

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36431023
I am interested about the "solution" ...
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now