December 10, 2009 at 3:23 pm
Aaron Bertrand has also blogged about it here.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
December 10, 2009 at 3:25 pm
clive-421796 (12/10/2009)
-------------------------
Have a question for you:
What if for a SQL report you want to use @StartDate AND @EndDate parameters, but also have the option for @StartHour AND @EndHour. How would you implement this. The default would be @StartDate AND @EndDate. If @StartHour AND @EndHour is chosen, then it will override @StartHour AND @EndHour.
Could you provide a sample of what you are describing here? Do mean to say that @StartDate and @EndDate get overridden?
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
December 10, 2009 at 3:28 pm
Lynn Pettis (12/10/2009)
Jeff Moden (12/10/2009)
clive-421796 (12/10/2009)
Cant open a .rar (winrar) file ? Ah come on Jeff, never heard of anyone working in IT for years that does not have winrar installed. Go to this URL. http://www.win-rar.com/download.html (1.4 MB)Heh... you're the one that needs the help... upload it as a zip file and I'll be happy to take a look at it. 😉
Jeff, just curious, what package do you use?
Pro version of WinZip... BUT... I just got done rebuilding the machine from the ground up and I don't have it installed yet.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2009 at 3:32 pm
Jeff Moden (12/10/2009)
Lynn Pettis (12/10/2009)
Jeff Moden (12/10/2009)
clive-421796 (12/10/2009)
Cant open a .rar (winrar) file ? Ah come on Jeff, never heard of anyone working in IT for years that does not have winrar installed. Go to this URL. http://www.win-rar.com/download.html (1.4 MB)Heh... you're the one that needs the help... upload it as a zip file and I'll be happy to take a look at it. 😉
Jeff, just curious, what package do you use?
Pro version of WinZip... BUT... I just got done rebuilding the machine from the ground up and I don't have it installed yet.
Okay. I was just wondering. Like I said, I have PKZip at home (paid for version) and I just used 7zip to extract it also (have that at work for doing command line archiving of files for ftping to other sites).
December 10, 2009 at 3:47 pm
Hi Jeff, I dont use winzip, I use winrar. Tried to download winzip, guess what ?
version 14 (latest) is 37 MB. So i looked for winzip 9 (about 4MB), every site I try redirects me to winzip 14 download, one link stated winzip, when trying to download it started downloading winrar.
December 10, 2009 at 3:50 pm
clive-421796 (12/10/2009)
Hi Lynn, instead of using:WHERE
tbl.YourDateColumn >= @StartDate
AND tbl.YourDateColumn < @EndDate
I prefer to use:
WHERE
tbl.YourDateColumn between @StartDate AND @EndDate.
-------------------------
Have a question for you:
What if for a SQL report you want to use @StartDate AND @EndDate parameters, but also have the option for @StartHour AND @EndHour. How would you implement this. The default would be @StartDate AND @EndDate. If @StartHour AND @EndHour is chosen, then it will override @StartHour AND @EndHour.
You really need to be careful using that, you may find you miss data or get some extra data. The method I just showed will always work giving you the data you request wether for a single day, a month, a quarter, a year, etc. It will return everything greater than or equal to the start of the period to everything less than the start of the next period.
For example, say that when you first wrote your query YourDateColumn contained data in the format yyyy-mm-dd 00:00:00.000, so you created your monthly query to look like this
WHERE
tbl.YourDateColumn BETWEEN @StartDate AND @EndDate --where @StartDate = '2009-11-01' @EndDate = '2009-11-30'
A few months later, the application is changed and now this column starts getting data that looks like yyyy-mm-dd HH:mm:ss.hhh.
If you don't change your query to account for the change in data, you miss everything entered after midnight on the last day.
If it had been coded:
WHERE
tbl.YourDateColumn >= @StartDate -- @StartDate = '2009-11-01'
AND tbl.YourDateColumn < @EndDate -- @EndDate = ''2009-12-01'
You wouldn't have that problem. A change in how data was captured wouldn't force a change in how you select data.
December 10, 2009 at 3:55 pm
Hi Circue
Usually you have @StartDate AND @EndDate parameters.
Now I want to also add @StartHour AND @EndHour.
The default would still be @StartDate AND @EndDate.
But if @StartHour AND @EndHour is chosen on the SQL report (the values populated by lets say 18 (18 PM) for @StartHour and say 5 (5 AM) for @EndHour,
then it should override both @StartDate AND @EndDate.
This way the either the user can select after hours for report or if you schedule the report you have the option for work hours or after hours.
December 10, 2009 at 4:20 pm
Hi Lynn,
the procs we have use the following variations:
@StartDate = '2009-11-01'
@StartDate = '2009-11-01 12:00:00'
@EndDate = '2009-12-01'
@EndDate = '2009-12-01 23:59:59'
2 months ago I had to use your method below:
WHERE
tbl.YourDateColumn >= @StartDate -- @StartDate = '2009-11-01'
AND tbl.YourDateColumn < @EndDate -- @EndDate = ''2009-12-01'
to get the correct data, luckily I always test procs/views for correct data returned, so I picked up the problem. Another issue I found is that some DBA's or developers used
WHERE table1.SUBSACC = isnull(@SUBSACC, table1.SUBSACC),
this is used to allow null values in SQL report. The catch is that it returns data for some customers, so if you test on one customer and you're not sure what data to get back you might think i works, whereas its not. By changing it back to WHERE table1.SUBSACC = @SUBSACC, it worked.
December 10, 2009 at 4:25 pm
clive-421796 (12/10/2009)
Hi Lynn,the procs we have use the following variations:
@StartDate = '2009-11-01'
@StartDate = '2009-11-01 12:00:00'
@EndDate = '2009-12-01'
@EndDate = '2009-12-01 23:59:59'
2 months ago I had to use your method below:
WHERE
tbl.YourDateColumn >= @StartDate -- @StartDate = '2009-11-01'
AND tbl.YourDateColumn < @EndDate -- @EndDate = ''2009-12-01'
to get the correct data, luckily I always test procs/views for correct data returned, so I picked up the problem. Another issue I found is that some DBA's or developers used
WHERE table1.SUBSACC = isnull(@SUBSACC, table1.SUBSACC),
this is used to allow null values in SQL report. The catch is that it returns data for some customers, so if you test on one customer and you're not sure what data to get back you might think i works, whereas its not. By changing it back to WHERE table1.SUBSACC = @SUBSACC, it worked.
This one, @EndDate = '2009-12-01 23:59:59' will work on a smalldatetime but it will potentially fail on datetime and datetime2 (new date/time data type in SQL Server 2008).
One thing I have learned, code defensively. What you are told will never change has a funny way of changing.
December 10, 2009 at 6:15 pm
clive-421796 (12/10/2009)
Hi Jeff, I dont use winzip, I use winrar. Tried to download winzip, guess what ?version 14 (latest) is 37 MB. So i looked for winzip 9 (about 4MB), every site I try redirects me to winzip 14 download, one link stated winzip, when trying to download it started downloading winrar.
BWAA-HAA!!! Now I'll play the same fiddle you played for me... :w00t:
Can't create a .zip (zip file) file? Ah, come on Clive, never heard of anyone working in IT for years that does not have a .zip file maker installed. :-P:hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2009 at 6:40 pm
BETWEEN includes the endpoints, so you might wind up with an extra day's worth of data using that if your columns don't contain time values.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
December 10, 2009 at 7:05 pm
clive-421796 (12/10/2009)
Hi Lynn, instead of using:WHERE
tbl.YourDateColumn >= @StartDate
AND tbl.YourDateColumn < @EndDate
I prefer to use:
WHERE
tbl.YourDateColumn between @StartDate AND @EndDate.
-------------------------
Have a question for you:
What if for a SQL report you want to use @StartDate AND @EndDate parameters, but also have the option for @StartHour AND @EndHour. How would you implement this. The default would be @StartDate AND @EndDate. If @StartHour AND @EndHour is chosen, then it will override @StartHour AND @EndHour.
Use DATETIME data types for both, add an hour to the EndDate using DATEADD and use the >= and < thingy the same way.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2009 at 10:33 pm
Lynn Pettis (12/10/2009)
What you are told will never change has a funny way of changing.
Never Say Never!
(Hehe, sorry, couldn't resist, but hopefully both the people that get that joke will share my amusement =))
December 14, 2009 at 12:39 pm
Hi Lynn, can you shed some light !!!
DECLARE @StartDate datetime, @EndDate datetime, @StartHour datetime, @EndHour datetime,
@liTimeOffset INT
SET @liTimeOffset = 120
SELECT DateAdd(n, @liTimeOffset, LocationDateTime)
FROM mytable
WHERE
LocationDateTime >= (CONVERT(VARCHAR(10), @StartDate, 120) + RIGHT(CONVERT(VARCHAR, @StartHour, 100),7))
AND
LocationDateTime < (CONVERT(VARCHAR(10), @EndDate, 120) + RIGHT(CONVERT(VARCHAR, @EndHour, 100),7))
LocationDateTime = '2009-11-01 08:13:02' (this is an example).
@StartHour and @EndHour is input parameters for stored procedure. When entering
2009-11-29 (@StartDate), 2009-11-30 (@EndDate), 22:00 (@StartHour), 03:00 (@EndHour)
the above works perfectly but I have one problem, if I want to use it as input parameters for SQL reporting services report it gives no result when I specify @StartHour and @EndHour as datetime or Integer. If I specify as string it does return results, but more rows are returned that it should.
How do I resolve issue ?
December 14, 2009 at 1:01 pm
How about some sample data and what it should look like? In this case, you can show me what the parameters to the WHERE clause should be based on the sample data.
Just from your post, not a clue.
Viewing 15 posts - 31 through 44 (of 44 total)
You must be logged in to reply to this topic. Login to reply