Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

BETWEEN syntax on INT date fields

Posted on 2011-03-11
14
Medium Priority
?
340 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 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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 2000 total points
ID: 36431023
I am interested about the "solution" ...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

886 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