Solved

Help : Difference between the 2 sql statements

Posted on 2009-07-06
8
196 Views
Last Modified: 2012-05-07
What is the Difference in this 2 sql statements.

SQL 1
============
select * from table_name a
where
      
      CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,START_DATE_OF_TRAVEL),0))) >=       CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,@START_DATE_OF_TRAVEL),0))) AND
      ( a.START_DATE_OF_TRAVEL >= @START_DATE_OF_TRAVEL and a.START_DATE_OF_TRAVEL <= @TRAVEL_END_DATE) AND
      datepart(dw, a.START_DATE_OF_TRAVEL) between 2 and 6

SQL 2
=========
select * from table_name  a where
      A.WORKFLOW_STATUS_ID=1 AND
      USER_ID <> @USER_ID AND
      CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,START_DATE_OF_TRAVEL),0))) >=       CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,@START_DATE_OF_TRAVEL),0))) AND
      ( a.START_DATE_OF_TRAVEL >= @START_DATE_OF_TRAVEL and a.START_DATE_OF_TRAVEL <= @TRAVEL_END_DATE)AND
      datepart(dw, a.START_DATE_OF_TRAVEL) in (1,7)

Please explain with eg. thanks
0
Comment
Question by:TECH_NET
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:kevin_u
Comment Utility
I assume you've detected this difference.

 A.WORKFLOW_STATUS_ID=1 AND
      USER_ID <> @USER_ID AND

In addition, the last item:
datepart(dw, a.START_DATE_OF_TRAVEL) between 2 and 6
matches dates of travel of monday thru friday

compared to
datepart(dw, a.START_DATE_OF_TRAVEL) in (1,7)
matches dates of travel of saturday and sunday

0
 

Author Comment

by:TECH_NET
Comment Utility
Just wanted to make sure that the dates of travel for the 1sql was monday thru friday
and the latter was saturday/sunday.

The
 A.WORKFLOW_STATUS_ID=1 AND
      USER_ID <> @USER_ID AND
was my typo. I just omiited it out in the first one.

Anyway, was just curious since i was able to retrieve any results. Maybe my dates format was messed up.(CONVERT(datetime)
0
 

Author Comment

by:TECH_NET
Comment Utility
the start_date_of_travel is stored in the following format
2009-07-06 15:11:03.000
2009-07-06 16:49:54.000
etc

I really want the date comparison on the date only.
With my sql, these dates get rounded to
2009-07-06 00:00:00.000
2009-07-06 00:00:00.000

and the sql query ( if replace the start-date with getdate() should return 2 records.
select CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,getdate()),0))) from COMMUTER_REQUESTS_VIEW a where
 
      CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,START_DATE_OF_TRAVEL),0))) >=       CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,getdate()),0))) AND
      ( a.START_DATE_OF_TRAVEL >= getdate() and a.START_DATE_OF_TRAVEL <= getdate()) AND
      datepart(dw, a.START_DATE_OF_TRAVEL) between 2 and 6

and that is not happening.
0
 
LVL 12

Expert Comment

by:kevin_u
Comment Utility
Are you asking another question?  I am unclear if you want me to do more.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:TECH_NET
Comment Utility
yes, the first queston arose from my question 2 which i assumed (if the explaination provided for the question 1 was the correct one - which i do agree), should have provide me with 2 records from my database.

0
 
LVL 12

Expert Comment

by:kevin_u
Comment Utility
if this is still part of your query, it won't work.
( a.START_DATE_OF_TRAVEL >= getdate() and a.START_DATE_OF_TRAVEL <= getdate()) AND

this will only work if start_date_of_travel is the current date and time exactly.
0
 

Author Comment

by:TECH_NET
Comment Utility
how can i get compare with the date part of the date
0
 
LVL 12

Accepted Solution

by:
kevin_u earned 500 total points
Comment Utility
You've already answered that yourself:

CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,START_DATE_OF_TRAVEL),0)))

>=  CONVERT(datetime, CONVERT(datetime, dateAdd(dd,datediff(dd,0,getdate()),0)))

That part will work just fine.

I'm not clear on what you're trying to accomplish.  Maybe some sample data would help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now