Solved

Advanced where  between but only for time, not for date.

Posted on 2009-04-14
6
194 Views
Last Modified: 2012-05-06
Hi expert!

Im using SQL Server 2005 and have question. Look on the picyure.  I need to apply criteria over date like this
WHERE  (dbo.LTimeSlots.StartDateTime BETWEEN dbo.LCampaignTimes.BTime AND dbo.LCampaignTimes.ETime)

But I need to check only for part of time (Hour, Minutes, second) and to ignore data part  (year, month, day) which is total different.   How to do this most effective way,  I have index over dbo.LTimeSlots.StartDateTime ?

Thank you!

sql-Date.JPG
0
Comment
Question by:dvplayltd
[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
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24142622
Hello dvplayltd,

This will be slow...

WHERE  CONVERT(varchar, dbo.LTimeSlots.StartDateTime, 8) BETWEEN CONVERT(varchar, dbo.LCampaignTimes.BTime, 8) AND CONVERT(varchar, dbo.LCampaignTimes.ETime, 8)

Regards,

Patrick
0
 
LVL 11

Accepted Solution

by:
N R earned 500 total points
ID: 24142643
Here you go:
WHERE (dbo.LTimeSlots.StartDateTime BETWEEN convert(datetime,dbo.LCampaignTimes.BTime,108) AND convert(datetime,dbo.LCampaignTimes.ETime,108))

Open in new window

0
 

Author Comment

by:dvplayltd
ID: 24142936
To Gallitin

Is it possible to forget to apply Convert also for StartDateTime ? I mean to be:

WHERE (convert(datetime,dbo.LTimeSlots.StartDateTime,108)  BETWEEN convert(datetime,dbo.LCampaignTimes.BTime,108) AND convert(datetime,dbo.LCampaignTimes.ETime,108))

And this convert convert to time, or to sring ? Becaues if is to string it will not work property ?
 
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24143721
dvplayltd said:
>>Is it possible to forget to apply Convert also for StartDateTime ? I mean to be:

Please look at my first post--that is exactly what I did :)

Patrick
0
 

Author Comment

by:dvplayltd
ID: 24143767
To matthewspatrick

I find best solution, here to you to know it :-). It is REALLY faster .. difrence is about 0.5 sec. over 0.6 sec. total time ..

      WHERE      (dbo.LTimeSlots.StartDateTime - CAST(FLOOR(CAST(dbo.LTimeSlots.StartDateTime AS float)) AS datetime)
          BETWEEN      (@BTime - CAST(FLOOR(CAST(@BTime AS float)) AS datetime))  AND
                              (@ETime - CAST(FLOOR(CAST(@ETime AS float)) AS datetime))  )
0
 

Author Comment

by:dvplayltd
ID: 24143780
Yes, but in first post you convert it to varchar, then compare it. I do not think that compare like nvarchar is egual to compare number and may result wrong result.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql Audit table 3 94
How can I get this column in my query? 2 62
Caste datetime 2 73
SQL Server Insert where not exists 24 57
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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