I'm new to CAML type queries. I'm trying to integrate sharepoint calendar data into a SSRS report. I pretty much need to do the following WHERE clause
(EventDate <= #StartDate AND EndDate >= #EndDate) OR (EventDate >= #StartDate AND EndDate <= #EndDate)
I need to include times into it as well. Can anyone please help me - as everything I have tried hasnt given the desired results.
Thanks!
Related
I need help understanding a bit of SQL.
SELECT *
FROM [MyTable]
WHERE CAST((GETDATE()-[MyDate]) as float) >= cast(0 as float) /24/60
AND CAST((GETDATE()-[MyDate]) as float) < cast(499999 as float) /24/60
I pretty much understand that its a date search, what I don't get is the float conversion. Why would you use that? Is that better for performance?
I understand that the first condition is: date higher or the same as 1900/01/01, what I just don't understand is what date is: 499999/24/60 and why is there the magic division?
Thanks for help and explanation.
Converting dates to floats is an old style way of dealing with dates. I think it might go back to more limited date functionality in SQL Server 2000. But it might also be a legacy of folks familiar with dates in Excel (where they are floats).
There is no performance advantage. In fact, such type conversions prevent the use of indexes.
For your two items of code, I think you want:
WHERE MyDate >= CONVERT(DATE, GETDATE()) AND
MyDate < DATEADD(DAY, 499999, '1899-12-30')
The last is arbitrary. A constant value such as:
WHERE MyDate >= CONVERT(DATE, GETDATE()) AND
MyDate < '3000-01-01'
makes more sense. You could use the actual date represented -- '3268-12-11' -- but that would probably confuse anyone reading the code.
EDIT:
On re-reading the question, the logic makes even LESS sense than it originally did. It seems to be something like:
WHERE MyDate < CONVERT(DATE, GETDATE()) AND
MyDate >= DATEADD(MINUTE, -499999, GETDATE())
And that causes an overflow.
I am building a report in SSRS where I need to be able to let our users filter on different date fields. They need to be able to select the date filter and then the start and end dates within that filter.
This is a simplified Example of what I am trying to achieve:
SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, Project.Start_Date, Project.Review_Date
FROM Project
WHERE #datetypefiler BETWEEN #startdate AND #enddate
Where the #datetypefiler will essentially equal the field the user wishes to filter on (i.e. Project.Update_Date or Project.Due_Date etc)
On SSRS i built parameters for #datetypefiler which could equal the various date fields and I set up the start date and end date filter as expected.
Unfortunately it does not seem as though I can parameterize the #datetypefiler in this way? I then attempted to use the Case keyword as so:
SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date, Project.Start_Date, Project.Review_Date
FROM Project
WHERE CASE
when #datetypefiler='Project.Update_Date' then Project.Update_Date BETWEEN #startdate AND #enddate
when #datetypefiler='Project.Due_Date' then Project.Due_Date BETWEEN #startdate AND #enddate
This isnt working for me either, I figure that instead of taking shots in the dark I should ask what the correct method would look like. Any advice on this would be greatly appreciated,
You could try something like this:
SELECT *
FROM Project
WHERE (#datetypefiler='Project.Update_Date'
AND Project.Update_Date BETWEEN #startdate AND #enddate)
OR (#datetypefiler='Project.Due_Date'
AND Project.Due_Date BETWEEN #startdate AND #enddate)
The OR will only evaluate the full statement if the #datetypefiler is matched.
Or you could try this
SELECT Project.ProjectID, Project.Name, Project.Update_Date, Project.Due_Date,
Project.Start_Date, Project.Review_Date
FROM Project
where
(CASE
when #datetypefiler = 'Project.Update_Date' then Project.Update_Date
when #datetypefiler= 'Project.Due_Date' then Project.Due_Date
END) BETWEEN #startdate AND #enddate
While both of other two answers are going to work just fine, they won't make use of index. I'd recommend to have two separate SELECT statements with two different queries based on your criteria, this way you can index your table both on Update_Date and Due_Date and actually use them to speed your queries up.
I am having a bit of an issue with my SSRS report I am trying to run. I am trying to have a report pull from the beginning of the day (to be ran daily) and the execution time.
I have:
(Rest of the SQL here)WHERE fa.ReceivedDate between #prmStartDate and %executionTime
In #prmStartDate currently I have =DateValue(today) which is creating an error when running.
Would this not work?
where ReceivedDate >= cast(getdate() as date)
and ReceivedDate < getdate()
If it will always use the current date, then you could use the GETDATE function in SQL server and you just need a way to strip off the time portion on the first date. A method I like is suggested here.
Your code would look like this:
(Rest of the SQL here)WHERE fa.ReceivedDate between DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) and GETDATE()
If you want to allow the user to choose a different day's data, just add a parameter instead of the GETDATE function:
(Rest of the SQL here)WHERE fa.ReceivedDate between #DateParam and DATEADD(dd, 1, #DateParam)
I am trying to build a vb.net application with a where clause to filter data using a DateTime field.
My table contains 5 fields and more than 10000 rows. I wanna use a DateTime field to find all the rows older than 7 years from now.
But this script will be re-used many times. So I don't wanna use this kind of where clause cause I don't wanna need to modify the where clause every time I wanna run the application :
select * from myTable WHERE myDateTimeField < '2006-09-07 00:00:00.000'
I'd like to find a way to write a where clause like this :
select * from myTable WHERE myDateTimeField "is older than 7 years from NOW"
I don't use VB.net very often (as you can see), so this thing is really bugging me
Just make use of DateTime:
Dim dateTime As DateTime
dateTime = DateTime.Now.AddYears(-7);
When you're building your SQL string just call ToString on your date (you can obviously format it however you need):
dateTime.ToString("MM/dd/yyyy");
SELECT * FROM myTable WHERE myDateTimeField < DATEADD(YEAR, -7, GETDATE())
That is if the data in the myDateTimeField is in the same local time zone of the sql server.
If your data is in UTC (which it often should be), then use:
SELECT * FROM myTable WHERE myDateTimeField < DATEADD(YEAR, -7, GETUTCDATE())
i think better would be provided you have SQL Server
strQuery = "select * from myTable WHERE myDateTimeField
<= DATEADD(YEAR, -7, GETDATE())"
What you are looking for is a DSL for datetime. Check out this blog post to get some ideas.
http://leecampbell.blogspot.com/2008/11/how-c-30-helps-you-with-creating-dsl.html
Good Luck
P.S.
If you need help translating it into vb.net just submit a comment.
select *
from Advertisements
where DepartureDate < DATEPART('dd.mm.yy', '09.10.2010');
but i get
Msg 1023, Level 15, State 1, Line 1
Invalid parameter 1 specified for datepart.
in plsql is this very simple here is so complicated...
Can someone tell me please how can i get all dates that are smaller than today.
You can use this to get the current date:
CONVERT(date, GETDATE())
See the documentation.
Can someone tell me please how can i get all dates that are smaller than today.
SELECT *
FROM Advertisements
WHERE DepartureDate < CONVERT(date, GETDATE())
You seem to be confusing DATEPART with FORMAT_DATE (which does not exist anyway).
DATEPART extracts certain part of a date. Exactly one part.
Dates that are smaller than today are < dbo.CropTime(getdate()), where CropTime is a function which can be written in different ways (such as those described in this question).
Or, in case you are using SQL Server 2008, it's as simple as < cast(getdate() as date).
Would that code really work in PL/SQL? The DATEPART in T-SQL function is used to extract individual portions of a date.
This will get you all the dates before now.
select * from Advertisements where DepartureDate < getdate()
If you're planning to hardcode the date (as your sample code suggests), you just need to format in a way that SQL Server will understand. eg.
select * from Advertisements where DepartureDate < '2010-10-09'
I've been told that date format works on every server regardless of its localization settings. It's certainly worked on every server I've tried it on - but I'm happy to be overruled :-)
What you are looking for I think is
select *
from Advertisements
where DepartureDate < Convert(Date, '09.10.2010', 102)
or possibly
SELECT *
FROM Advertisements
WHERE DepartureDate < Cast(CURRENT_TIMESTAMP as date)
DatePart is used for getting components of the date such as the month, year or day. To get dates that are smaller (older) than now I would do this.
select * from Advertisements where DepartureDate < GetDate();
If I wanted Departure dates that were yesterday or before I could do this.
select * from Advertisements where DepartureDate < Convert(DateTime,Convert(Char(10),GetDate(),121));
or
select * from Advertisements where DepartureDate < Convert(DateTime,floor(convert(int,GetDate())))