I need to write a stored procedure in which i will get begin date and end date. for eg: start date is 05/07/2015 and end date is 28/08/2015. I need to write a query which will fetch details between 01/07/2015 and 31/08/2015. It should fetch details between 1st day of month selected for begin date and last day of month selected for end date.
This is what I tried, but it isn't working:
DATEDIFF(month,'2014-06-05','2014-08-05')
You can compare the dates using the common operators "<" and ">",
Example:
select * from table_name where '2014-06-05' < datefield and datefield < '2014-08-05'
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = convert(DATE, '20140705' ,112)
SET #EndDate = convert(DATE, '20140828' ,112)
SELECT * FROM [TableName] WHERE [TableName].DateField BETWEEN
dateadd(d,-(datepart(d, #StartDate)-1),#StartDate) AND
dateadd(d,-1,dateadd(m,1, dateadd(d,-(datepart(d, #EndDate)-1),#EndDate)))
It sounds like you need to find the first day of the month for '05/07/2015' and the last day of the month for '28/08/2015', then use those as the limits for your where clause.
This will easily convert a date to the first of the month:
declare #firstDate datetime = '2015-07-05'
declare #firstOfMonth datetime = dateadd(month,datediff(month,0,#firstDate),0)
select #firstDate
-- returns '2015-07-01 00:00:00.000'
To get to the last day of a month, use this:
declare #lastDate datetime = '2015-08-28'
declare #lastOfMonth datetime = dateadd(second,-1,dateadd(month,datediff(month,0,#lastDate)+1,0))
select #lastOfMonth
-- returns '2015-08-31 23:59:59.000'
Related
I require to calculate the Last day of previous week and last of day current week in Multidimensional Cube.I need to have in Cube calculation
For example: I have Time dimension with Hierarchies as below
Year-Quarter-month-date
Year-Quarter-month
Year-Quarter-week
Year-Week.
I have Time Week dimension- which has hierarchy Time-week and my View show below data
Week-2018-W18
WeekNumberOfYear-2018-W18
Year-18
YearNo-2018
StartDate-2018-04-30 00:00:00.000
EndDate-2018-05-06 23:59:59.997
CurrentWeek-1
Current Week is set flag set. As per above data.
If I run the report today(03/05/2018)-Thursday.
previous week last working day should show- 2018-04-27- Friday
Current week last day should be-2018-05-04-Friday.
Please let me know how to calculate the dates via cube calculation, I am able to find the previous year with Parallel Period function.
This will work.
You need to just mention last day name.
Declare #Pre_last_Dayname datetime, #cur_last_Dayname datetime, #date datetime, #found datetime, #dayname varchar(14)
set #date = GETDATE()
set #dayname = 'Friday'
WHILE #Pre_last_Dayname is null
BEGIN
set #date = #date - 1
select #Pre_last_Dayname = #date
where DATENAME(dw,#date) = #dayname
END ;
set #date = GETDATE()
WHILE #cur_last_Dayname is null
BEGIN
select #cur_last_Dayname = #date
where DATENAME(dw,#date) = #dayname
set #date = #date + 1
END
select concat(format(#Pre_last_Dayname ,'dd/MM/yyyy'),'-',DATENAME(dw,#Pre_last_Dayname) )'last_day of_last_week'
, concat(format(#cur_last_Dayname ,'dd/MM/yyyy'),'-',DATENAME(dw,#cur_last_Dayname) )'last_day of_current_week'
, concat(format(GETDATE() ,'dd/MM/yyyy'),'-',DATENAME(dw,GETDATE()) )'current_date'
Use this for getting previous week date.
Declare #CurWkFromDate DateTime,
#CurWkTodate DateTime,
#PrvWkFromdate DateTime,
#PrvWkTodate DateTime,
#Ondate DateTime = '2018-05-03'
select #CurWkFromDate = dbo.Lynk_Current_WeekStartDate(#Ondate),
#CurWkTodate = dbo.Lynk_Current_WeekEndDate(#Ondate),
#PrvWkFromdate = dbo.Lynk_Current_WeekStartDate(Dateadd(dd,-1,#CurWkFromDate)),
#PrvWkTodate = dbo.Lynk_Current_WeekEndDate(#PrvWkFromdate)
select #CurWkFromDate,#CurWkTodate,#PrvWkFromdate,#PrvWkTodate
First create the function in DB and try this.
CREATE Function Lynk_Current_WeekStartDate
(
#processDate datetime
)
Returns DateTime
As
Begin
Declare #FromDate DateTime
Set #FromDate=dateadd(d,0,dateadd("ww",datediff(wk,'01/01/1900',DATEADD(dd,-1,#processDate)),'01/01/1900')) --Monday
Return(#FromDate)
End
CREATE Function Lynk_Current_WeekEndDate
(
#Processdate datetime
)
Returns DateTime
As
Begin
Declare #FromDate DateTime
Declare #ToDate DateTime
Set #FromDate=dbo.Lynk_Current_WeekStartDate(#Processdate)
Set #Todate=dateadd(ss,-1,dateadd(d,5,#FromDate))
Return(#ToDate)
End
Example:
select dbo.Lynk_Current_WeekStartDate('2018-05-03')
select dbo.Lynk_Current_WeekEndDate('2018-05-03')
I have a table of dates which includes every day as a separate row between a #startDate and an #endDate.
I need to remove all dates apart from the ones which match a #dayOfMonth, say the 16th as an example.
But I don't need to keep every row which contains a 16th of the month. I have to factor in another variable, #everyNMonths. So if #everyNMonths was set to 2, and my #startDate was '2016-10-13' and #endDate was '2017-03-20' I would want to keep:
2016-10-16
2016-12-16
2017-02-16
Not sure how I can achieve this. Thanks for any help.
This should work:
DECLARE #StartTime DATE = '2015-11-01'
DECLARE #EndTime DATE = '2016-10-01'
DECLARE #Interval INT = 1
DECLARE #dayOfMonth INT = 16
--CREATE Table #TempTimes
SELECT StartTime
FROM
(
SELECT StartTime,ROW_NUMBER() OVER (ORDER BY StartTime) As Num
FROM
(
SELECT DISTINCT StartTime
FROM #TempTimes
WHERE DATEPART(DD,StartTime) = #dayOfMonth
AND StartTime BETWEEN #StartTime AND #EndTime
)t
)s
WHERE (Num - 1)%#Interval = 0
DECLARE #currDate DATETIME
DECLARE #days INT
SELECT DATEADD(dd ,#days ,#currDate)
WHERE #days = 10
AND #currDate = GETDATE()
Assuming SQLServer, this will add 1 day to current date. You can apply further functions to extract only date part etc.
SELECT DATEADD(day, 1, current_timestamp);
Replace current_timestamp with any other date you want.
SQLFiddle example here
It is so simple as below,
DECLARE #days AS INT=10
DECLARE #currDate AS DATE=GETDATE()
SELECT DATEADD(DAY ,#days ,#currDate) AS AddedDate
I'm writing a stored procedure in sql!
I have to get records in the particular date.
I am using this query:
Declare #FromDate datetime
set #FromDate = '06/02/2014'
select * from Table where Date = #FromDate
Actually, in the Database there are 10 records in that date, but it is showing only two records because the #FromDate is taking like this 06/02/2014 00:00:00.000
If I write the query like this it means it works correctly!
select * from Table
where Date between '2014-08-28 00:00:00.000' and '2014-08-28 23:59:59.999'
How to solve this? I need to get all the records in that particular date.
Please help me !
If #FromDate is of data type datetime and Table.Date is also of data type datetime then:
Declare #FromDate datetime = '2014-06-02';
Select Table.Date
From Table
Where Table.Date >= #FromDate And Date < DateAdd(day, 1, Table.Date)
Above, we create an inclusive lower boundary (anything equal to or later than 2014-06-02) and an exclusive upper boundary (anything earlier than 2014-06-03), but with a variable defined just once. So, effectively the query checks 2014-06-02 <= FromDate < 2014-06-03.
If you convert DateTime to Nvarchar your issue would be solved.
Try this query:
Declare #Date datetime='2014-08-28 00:00:00.000'
select * from Table
where CONVERT(nvarchar(20),Date,105) = CONVERT(nvarchar(20),#Date,105)
select * from sampleTable
where CONVERT(VARCHAR(20),DateCreated,101)
= CONVERT(VARCHAR(20),CAST('Feb 15 2012 7:00:00:000PM' AS DATETIME),101)
I want to compare date without time
Is above query is ok? or other better solution you suggest
I am using SQL Server 2005
Date saved in UTC format on server
Users against this data belongs different timezone
Simple Cast to Date will resolve the problem.
DECLARE #Date datetime = '04/01/2016 12:01:31'
DECLARE #Date2 datetime = '04/01/2016'
SELECT CAST(#Date as date)
SELECT CASE When (CAST(#Date as date) = CAST(#Date2 as date)) Then 1 Else 0 End
Don't use convert - that involves strings for no reason. A trick is that a datetime is actually a numeric, and the days is the integer part (time is the decimal fraction); hence the day is the FLOOR of the value: this is then just math, not strings - much faster
declare #when datetime = GETUTCDATE()
select #when -- date + time
declare #day datetime = CAST(FLOOR(CAST(#when as float)) as datetime)
select #day -- date only
In your case, no need to convert back to datetime; and using a range allows the most efficent comparisons (especially if indexed):
declare #when datetime = 'Feb 15 2012 7:00:00:000PM'
declare #min datetime = FLOOR(CAST(#when as float))
declare #max datetime = DATEADD(day, 1, #min)
select * from sampleTable where DateCreated >= #min and DateCreated < #max
SELECT .......
FROM ........
WHERE
CAST(#DATETIMEVALUE1 as DATE) = CAST(#DATETIMEVALUE2 as DATE)
The disadvantage is that you are casting the filter column.
If there is an index on the filter column, then, since you are casting, the SQL engine can no longer use indexes to filter the date more efficiently.
Description
Don't convert your Date to a varchar and compare because string comparisson is not fast.
It is much faster if you use >= and < to filter your DateCreated column.
If you have no parameter (like in your sample, a string) you should use the ISO Format <Year><Month><Day>.
Sample
According to your sample
DECLARE #startDate DateTime
DECLARE #endDate DateTime
SET #startDate = '20120215'
SET #endDate = DATEADD(d,1,#startDate)
SELECT * FROM sampleTable
WHERE DateCreated >= #startDate AND DateCreated < #endDate
More Information
MSDN - DATEADD (Transact-SQL)
Use 112 CONVERT's format
select *
from sampleTable
where
CONVERT(VARCHAR(20),DateCreated,112)
= CONVERT(VARCHAR(20),CAST('Feb 15 2012 7:00:00:000PM' AS DATETIME),112)
or
if your sql server version 2008+ use DATE type
select * from sampleTable
where CONVERT(DATE,DateCreated)
= CONVERT(DATE,CAST('Feb 15 2012 7:00:00:000PM' AS DATETIME))
declare #DateToday Date= '2019-10-1';
print #DateToday;
print Abs(datediff(day, #DateToday,CAST('oct 1 2019 7:00:00:000PM' AS DATETIME))) < 3
this is compare whin 3 days.
i test this on SQL Server 2014, it works.
select * from sampleTable
where date_created ='20120215'
This will also compare your column with the particular date
without taking time into account