Select Statement Gathering Dates - date-arithmetic

I have a form where I submit a Start and End date to book of holidays, I then send the value's across to SQL, now i'm a bit stuck because what I need to do is get the dates between the start and end date.
Can anyone help me with this I just need a calculation for my select statement to transfer all the dates between and on the start and end date.
Thanks in advance to your answers/replies :)

Try this:
DECLARE #FromDate datetime
DECLARE #ToDate datetime
SELECT #FromDate=FromDateCol FROM TableName
SELECT #ToDate=ToDateCol FROM TableName
WITH cte AS
(
SELECT CAST(#FromDate AS DATETIME) DateValue
UNION ALL
SELECT DateValue + 1
FROM cte
WHERE DateValue + 1 < #ToDate
)
SELECT DateValue
FROM cte
OPTION (MAXRECURSION 0)

That should not be to difficult to difficult to figure if you are using SQL server! Try this website it is good documentation on how to retrieve those dates that you need.
These are the select statements:
start date (PRSTDATE) and the end date (PRENDATE)
SELECT statement
docs.oracle.com/javadb/10.6.2.1/.../rrefsqlj41360.htm...‎

You asked for the URL, all I have is the web address! Paste it into your search engine. I Hope it helps...
http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj41360.html

Hi just do a while loop,
While #Date <= #EndDate
Begin
Set #Date = DATEADD(DD, 1, #Date)
Select #Date
End

Related

Given datetime, filter results by month of that datetime

For sql server 2008: what is the best way to filter results, to return only results that are in the same month as a certain date?
The best I could come up with is the following:
-- set up test data
DECLARE #TABLE1 AS TABLE (
ID INT,
STRING VARCHAR(MAX),
DATECOLUMN DATETIME
)
INSERT INTO #TABLE1
SELECT
0 ID,
'TABLE1 0' STRING,
CONVERT(DATETIME, '2016-10-13 12:45:00', 102) DATECOLUMN
UNION ALL SELECT 1, 'TABLE1 1', CONVERT(DATETIME, '2016-9-13 12:45:00', 102)
UNION ALL SELECT 2, 'TABLE1 2', CONVERT(DATETIME, '2016-10-1 00:00:00', 102)
UNION ALL SELECT 3, 'TABLE1 3', CONVERT(DATETIME, '2016-10-31 23:59:59', 102)
-- set up constraint
DECLARE #SOMEDATE DATETIME = CONVERT(DATETIME, '2016-10-13 12:45:00', 102)
-- filter
SELECT * FROM #TABLE1
WHERE MONTH(DATECOLUMN) = MONTH(#SOMEDATE)
Is this the best way?
Your query will return records where the month of DateColumn matches the month of #SomeDate, although (as #Jayvee points out) this would be true regardless of the year. So if that's what you want, your solution is fine.
But you asked whether that was the best solution. I would say no, because this won't take advantage of any index you may have that includes DateColumn.
So this is what I would do, if I were on SQL Server 2008:
declare #someDate datetime = GetDate()
declare #startDate date
declare #endDate date
-- I want the first day of the month, at 12:00:00AM (no time component).
-- The DateAdd expression subtracts days, and converting to the Date data type
-- ensures that I don't have a time component hanging around.
set #startDate = convert(date, DateAdd(day,- DatePart(day, #someDate) + 1, #someDate))
-- I want the first day of the next month. This is easy:
set #endDate = DateAdd(month, 1, #startDate)
-- Here's my actual query, that can take advantage of indexes that include DateColumn:
select *
from Table1
where DateColumn >= #startDate and DateColumn < #endDate
Note that I said >= for start date but < for the end date. That ensures that I'll pick up any date entries for the last day of the month that have a non-zero time component, but won't go into the next month.
The best way for a long-term solution is to create a date dimension table (you can find scripts out there to auto-generate one). The date dim table will just contain a list of dates as far back and forward as you care to go, and it includes columns like DayOfWeek, QuarterOfYear, YYYYMM, etc., so you can join CAST(YOURDATE AS DATE) to the date dim's date and pull nifty date info. Here you can join both of your tables to the date dim and use WHERE t1.YYYYMM = t2.YYYYMM.

Count first day of month between two dates

Could someone advise how I can go about counting the number of first day of month between two dates?
For example: 02/01/2015 to 05/05/2015 will count as 4.
It can be accomplished easily like this:
DECLARE #sd DATE = '02/02/2015', #ed DATE = '05/01/2015'
SELECT DATEDIFF(mm, #sd, #ed) + CASE WHEN DAY(#sd) = 1 THEN 1 ELSE 0 END
If you have a sequence of dates stored somewhere (like in a calendar table) you can easily count the dates matching the day(date) predicate. If you don't have any suitable table with a date sequence you can use a recursive common table expression to generate one on the fly like this:
declare #start date
set #start = '02/01/2015'
declare #end date
set #end = '05/05/2015'
;with dates (date) as (
select #start date
union all
select dateadd(day, 1, date) as date from dates where date < #end
)
select count(*) no_of_firsts from dates where day(date) = 1
Sample SQL Fiddle

Most efficient way of flooring todays date

This seems like overkill but is the only way I have been able to floor todays datetime to 00:00:00.000 at database level:
select CAST(FLOOR(CAST(CURRENT_TIMESTAMP AS float)) AS DATETIME)
I have tried using:
select FLOOR(getdate())
But get the following message:
Implicit conversion from data type datetime to float is not allowed. Use the CONVERT function to run this query.
Can anyone recommend another way of doing this?
Since you are using SQL Server 2008 you could make use of the date data type.
declare #Today date
set #Today = getdate()
select #Today
Or without the variable.
select cast(getdate() as date)
If you need to have the value as a datetime just cast it back to a datetime.
select cast(cast(getdate() as date) as datetime)
There are a lot of ways of doing this i have seen the floor one before. Here are a few more.
select cast(cast(CURRENT_TIMESTAMP as date) as datetime)
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SELECT CAST(CAST(CURRENT_TIMESTAMP - 0.50000004 AS int) AS datetime)
I normaly do the Cast to date version.

How to use DATENAME in WHERE clause

Am using Sql Server 2008, I have a column named Date in my table, and I want to get the datas for the particular date.... I need to give this Date in my WHERE condition.
for example, if I want to get the records for the particular month in the given date, how can I use this Date in WHERE condition.
DATANAME(MONTH,'#Date')
if I give like this in my query I can get the month from the given DATE, the same way I tried by putting in WHERE condition like,
WHERE DATE= DATANAME(MONTH,'#Date')
here it reports conversion error...how can I display the datas for a particular month, can anyone help me
If you want a month of data for a table you should check against an interval. The query is not able to use indexes on the date column if you are applying functions on the column.
Use something like this to get data for April 2012.
-- The date parameter
declare #Date datetime
set #Date = '2012-04-11'
declare #FromDate datetime
declare #ToDate datetime
-- set FromFate to first of april
set #FromDate = dateadd(month, datediff(month, 0, #Date), 0)
-- set ToDate to first of may
set #ToDate = dateadd(month, 1+datediff(month, 0, #Date), 0)
select *
from YourTable
where [Date] >= #FromDate and [Date] < #ToDate
If you want to show data for a particular year and month you can use the YEAR and MONTH functions:
SELECT ...
FROM ...
WHERE YEAR(mydate) = 2012 AND MONTH(mydate) = 3 -- March, 2012
To me it seems that your field Date is not of type varchar or nvarchar, so using a condition where a Datetime = string is obviously wrong.
Have you tried
WHERE DATE= #Date
Shouldn't it be:
DATENAME(MONTH, #Date)
Instead of:
DATANAME(MONTH,'#Date')
(Notice "DATA" vs "DATE" and #Date isn't in quotations)
Then to use this against a date/datetime column you would have to cast both sides like below:
WHERE datename(Month, [Date]) = datename(Month, [Date])
Warning: The above does not use any indexes so isn't as efficient as "WHERE Date = Date"
First: Remove '' from variable. #Date, not '#Date'
If you want to find dates from specific month. (You have to remember about year condition also)
WHERE DATANAME(MONTH, #Date) = 'April'
if you want to find exact date:
WHERE DATE = #date

Need to find date's between 2 date's in SQL Server 2008

I need help with query to find all the dates that between 31/12/2009 and 31/02/2010
In SQL Server 2008
i try this:
SELECT convert(varchar(50), MyDate, 103)
where convert(varchar(50), MyDate, 103) >= '31/12/2009' and convert(varchar(50), MyDate, 103) <='31/02/2010'
but it give me wrong result
why ?
I had a different interpretation of the question: "how to generate all the dates between a certain range?"
Here's a solution to that:
--define start and end limits
Declare #todate datetime, #fromdate datetime
Select #fromdate='2009-03-01', #todate='2009-04-10'
;With DateSequence( Date ) as
(
Select #fromdate as Date
union all
Select dateadd(day, 1, Date)
from DateSequence
where Date < #todate
)
--select result
Select * from DateSequence option (MaxRecursion 1000)
There is a nice article that shows how to generate sequences (numbers, dates, times) using CTEs.
Edit:
After clarification, the issue seems to be the date format being input: dd/mm/yyyy.
SQL Server expects the format mm/dd/yyyy.
I would simply transform it before running the select statement:
-- Assuming two variables, #inputFromDate and #inputToDate, in the format of dd/mm/yyyy...
declare #fromDate varchar(10), #toDate varchar(10)
set #fromDate =
substring(#inputFromDate, 3, 2) + '/' +
substring(#inputFromDate, 1, 2) + '/' +
substring(#inputFromDate, 7, 4)
set #toDate =
substring(#inputToDate, 3, 2) + '/' +
substring(#inputToDate, 1, 2) + '/' +
substring(#inputToDate, 7, 4)
select * from SomeTable where dateCol >= #fromDate and dateCol < #toDate
-- you can change the < or >= comparisons according to your needs
If the MyDate column is a datetime, as it appears to be, then it's already in the right "format". Don't convert it to a varchar(50) in the predicate condition - this makes your query non-sargable and will kill performance on any indexes you might have.
Instead, take your parameters as date or datetime instances:
SELECT ...
FROM MyTable
WHERE MyDate >= #BeginDate
AND MyDate <= #EndDate
Your query should not depend on a specific date format in the input parameters - those parameters are not varchar types, they are datetime (or date). When you run this query or stored procedure from whatever environment the application is in and supply binding parameters (you are using bind parameters, right?), said library will automatically handle any formatting issues.
If you try to use the >= and <= operators on character representations of dates, with any format other than the ISO standard yyyymmdd, you will get the wrong results, because the alphabetical order is different from the temporal order. Don't do this.
If you simply need to write an ad-hoc query, i.e. this isn't being run from any programming environment, then simply do not use the dd/mm/yyyy format. Use the ISO date format instead; it is unambiguous and implicitly convertible to datetime values:
SELECT ...
FROM MyTable
WHERE MyDate >= '20091231'
AND MyDate <= '20100231'
Honestly, no other solution is acceptable in my mind. For ad-hoc queries, always use the unambiguous ISO standard for dates. For applications connecting to the database, always use bind parameters. If you're doing anything else, you're writing code that's either unreliable, insecure, or both.
BOL is always a good reference...
Start with BETWEEN
Did you try this:
DECLARE #FROMDATE DATETIME
DECLARE #TODATE DATETIME
SET #FROMDATE = GETDATE()
SET #TODATE = GETDATE()+7
;WITH DATEINFO(DATES) AS (SELECT #FROMDATE UNION ALL SELECT DATES + 1 FROM DATEINFO WHERE DATES < #TODATE)
SELECT * FROM DATEINFO OPTION (MAXRECURSION 0)