Know what the following SQL code is doing - sql

What does the following code do??
Declare #StartDate datetime; -- figure
Declare #EndDate datetime; -- figure
Declare #CurrentDate datetime
Set #CurrentDate = #StartDate
While #CurrentDate <= #EndDate
Begin
Insert Into dbo.Tbl_Time_Dimension
Values (#CurrentDate,
year(#CurrentDate),
datepart(Quarter, #CurrentDate),
month(#CurrentDate),
datepart(week, #CurrentDate),
day(#CurrentDate))
Select #CurrentDate = DateAdd(dd, 1, #CurrentDate)
End

This code will iterate from #StartDate to #EndDate skipping by day and will insert a row into a table called Tbl_Time_Dimension.
The fields being saved into this table are : Date, Year, Quarter, Month, Week and Day for each date in the date range.
Declare #StartDate datetime; --figure
Declare #EndDate datetime; --figure
Declare #CurrentDate datetime
-- Starts the iterator on #StartDate
Set #CurrentDate=#StartDate
-- Iterates until #EndDate
While #CurrentDate<=#EndDate
Begin
-- Saves data in table
Insert Into dbo.Tbl_Time_Dimension
Values (#CurrentDate, year(#CurrentDate),
datepart(Quarter,#CurrentDate),
month(#CurrentDate),
datepart(week,#CurrentDate),
day(#CurrentDate))
-- Increments the date by 1 day
Select #CurrentDate=DateAdd(dd,1,#CurrentDate)
End

this query gets two date
and for each day between startdate and enddate insert that date into Tbl_Time_Dimension as well as it's month , week , Day and quarter

Related

How to find week Number based on current date and what are the dates in that week

I need to find out Week Number based on current date and what are the dates in that week.
Let say example, Current date is 27-Dec-2020, then I need to find out week no i.e. 53 and what are dates i.e. 28-Dec-2020, 29-Dec-2020....03-Jan-2021.
My expected output columns would be:
WeekNo Date Day
declare #date date = '30-Dec-2020'; --'20210101'
select #date as _date,
datepart(iso_week, #date) as isoweek,
--iso week starts on previous monday. weekday of monday is always 2 when accounting for ##datefirst
dateadd(day, -(7-2+datepart(weekday, dateadd(day, ##datefirst, #date)))%7, #date) as isoweekstartdate,
--isoweekenddate(inclusive) = add 6 days to isoweekstartdate
dateadd(day, 6, dateadd(day, -(7-2+datepart(weekday, dateadd(day, ##datefirst, #date)))%7, #date)) as isoweekenddate;
select
datepart(iso_week, #date) as isoweek,
dateadd(day, n.num, dateadd(day, -(7-2+datepart(weekday, dateadd(day, ##datefirst, #date)))%7, #date)) as isoweekdate
from
(
values (0),(1),(2),(3),(4),(5),(6)
) as n(num);
Using a simple Loop
DECLARE #date DATE, #WeekNo TINYINT, #Start TINYINT,#End TINYINT, #StartDate DATE,#EndDate DATE, #Cdate DATE
SET #date='27-DEC-2020'
SET #StartDate = DATEADD(DAY,-7,#date)
set #EndDate = DATEADD(DAY,7,#date)
SET #WeekNo = DATEPART(WEEK,#date)
SET #Start = 1
SET #End = DATEDIFF(DAY,#StartDate,#EndDate)
DECLARE #dates TABLE(WeekNo TINYINT, [Date] DATE, [Day] VARCHAR(20))
WHILE(#Start <=#End)
BEGIN
SET #Cdate = DATEADD(DAY,#Start,#StartDate)
IF DATEPART(WEEK,#Cdate)=#WeekNo
INSERT #dates VALUES(#WeekNo, #Cdate, DATENAME(WEEKDAY , #Cdate))
SET #Start = #Start + 1
END
SELECT * FROM #dates

How to write sql query for adding days given by the user to the current date where current date and days to be added need to be taken as parameters

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

How to get dates between time range in sql server

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'

get last date in tsql

i show you my logic is getting error only when month is December
declare #startDate datetime
declare #endDate datetime
set #startDate = convert(varchar(2),#month)+'/1/'+ convert(varchar(4),#year)
set #endDate = dateadd(DD,-1,(convert(varchar(2),#month+1)+'/1/'+convert(varchar(4),#year)))
while(#startDate < #endDate+1)
begin
insert into #tempday
select #startDate
set #startDate = dateadd(day, 1, #startDate )
end
please help
Once you have #startdate, use:
set #enddate = dateadd(day,-1,dateadd(month,1,#startdate))
And I don't think you mean plsql...
There are other things you could think about too, such as not using a while-loop. Why not query a table that has plenty of rows (such as sys.all_columns) and use:
insert #tempday
select top (datediff(day,#startdate,#enddate)+1)
dateadd(day,row_number() over (order by (select 1))-1,#startdate)
from sys.all_columns;
In case of December #month+1 will get you 13 which is not a valid month number

SQL Separate hours in Start and EndDate

I'm need a help to create a Query. My problem is I have a StartDate and EndDate and need separate this in blocs of 60 minutes.
DECLARE #STARTDATE AS SMALLDATETIME
DECLARE #ENDDATE AS SMALLDATETIME
SET #STARTDATE = '2012-11-21 11:03:00'
SET #ENDDATE = '2012-11-21 13:04:00'
I need the return:
Hour, Time
11 , 57
12 , 60
13 , 04
You could use a recursive CTE. For example:
declare #startDate datetime = '2012-11-21 22:05:00'
declare #endDate datetime = '2012-11-22 01:06:00'
; with TimeList as
(
select #startDate as dt
union all
select dateadd(hour, 1, dateadd(hour, datediff(hour, 0, dt), 0))
from TimeList
where dateadd(hour, 1, dt) < #endDate
)
select dt
from TimeList
union all
select #endDate
The snippet dateadd(hour, datediff(hour, 0, dt), 0) removes the hours and minutes from a date. It does so by calculating the number of hours since date 0 and then adding that number of hours to date 0.
Live example at SQL Fiddle.
I unsure if i understood you but this will return the hour and minute after your start date at 60 min intervals.
DECLARE #STARTDATE AS SMALLDATETIME
DECLARE #ENDDATE AS SMALLDATETIME
DECLARE #time AS TABLE(id int identity(1,1), [hour] int, [time] int)
SET #STARTDATE = '2012-11-21 11:03:00'
SET #ENDDATE = '2012-11-21 13:04:00'
WHILE #STARTDATE < #ENDDATE
BEGIN
SELECT #STARTDATE = DATEADD(MINUTE,60,#STARTDATE)
INSERT INTO #time (hour,time)
VALUES(DATEPART(HOUR,#STARTDATE),DATEPART(MINUTE,#STARTDATE))
END
SELECT * FROM #time
You coan do it in three pieces. First piece is for the first hour, 60 minus the minute value, 2nd piece is time=60 for all hours between start+1 and end, third piece is end minutes
and then insert them into a temp table, as abstractChaos has done.
Insert into temp table like AbstractChaos:
DECLARE #STARTDATE AS SMALLDATETIME
DECLARE #ENDDATE AS SMALLDATETIME
DECLARE #TIME AS TABLE(id INT IDENTITY(1,1), [HOUR] INT, [TIME] INT)
SET #STARTDATE = '2012-11-21 11:03:00'
SET #ENDDATE = '2012-11-21 13:04:00'
INSERT INTO #TIME (HOUR,TIME)
VALUES (datepart(HOUR,#startdate) ,60 - datepart(MINUTE,#startdate) )
WHILE #STARTDATE < #ENDDATE
BEGIN
SELECT #STARTDATE = DATEADD(MINUTE,60,#STARTDATE)
INSERT INTO #TIME (HOUR,TIME)
VALUES(datepart(HOUR,#STARTDATE) , 60)
END
INSERT INTO #TIME (HOUR,TIME)
VALUES(datepart(HOUR,#enddate) , datepart(MINUTE,#startdate))