Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BETWEEN syntax on INT date fields

Posted on 2011-03-11
14
Medium Priority
?
338 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article I will describe the Backup & Restore 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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

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