Solved

BETWEEN syntax on INT date fields

Posted on 2011-03-11
14
336 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql2016-WIn10: standard,for SQL servc-account.. 51 55
How can I get the entire database script? 7 34
SQL Server Express or Standard? 5 63
Can a Trigger trigger a Trigger? 4 47
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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