Solved

BETWEEN syntax on INT date fields

Posted on 2011-03-11
14
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 21

Expert Comment

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

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 143

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 41

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
 
LVL 41

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 143

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 143

Accepted Solution

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

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 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