Let the user select how many days of data he wants - sql-server

declare #Days varchar(max)
set #Days = '-7'
select
dateadd(hour,datepart(hour,Timestamp + GETDATE() - GETUTCDATE()),cast(CAST((Timestamp + GETDATE() - GETUTCDATE()) as date) as datetime)) as [Time]
from [Employee]
where dateadd(hour,datepart(hour,Timestamp + GETDATE() - GETUTCDATE()),cast(CAST((Timestamp + GETDATE() - GETUTCDATE()) as date) as datetime)) >= DATEADD(day,' + #Days + ', GETDATE()))
I want the user to select the number of days(#Days) of data he wants. So if he wants the data for last 15 days, all he has to do is set #Days = '-15'. Timestamp is the date along with time Column in my Employee table but Timestamp is UTC. I have written the query above and it is returning some data but I am confused if the query is correct or not?

I think the following simple query should do the trick.
declare #Days INT = -7; --<-- Use int not varchar
SELECT *
FROM [Employee]
WHERE CAST([Timestamp] AS DATE) >= CAST(DATEADD(day, #Days, GETUTCDATE()) AS DATE);
DATEADD() function's 2nd parameter is an int, you can pass the variable #Days to the function as it is.

Related

Stored procedure to add 30 days using DATEDIFF within while loop condition in Date Dimension table

I want to add 30 consecutive days of data in my Date Dimension table using DATEDIFF() but I am getting blank result. Can you please help me correct the code below to get the desired result?
CREATE TABLE dbo.dateDimension (
DateKey INT NOT NULL
,DateValue DATE NOT NULL
,CYear SMALLINT NOT NULL
,CMonth TINYINT NOT NULL
,CONSTRAINT PK_DimDate PRIMARY KEY ( DateKey )
);
GO
CREATE PROC dbo.dateTest
#StartDate DATETIME
AS
WHILE (DATEDIFF(day, #StartDate, GETDATE()) <=30)
BEGIN
INSERT into dbo.dateDimension
SELECT CAST( YEAR(#StartDate) * 10000 + MONTH(#StartDate) * 100 + DAY(#StartDate) AS INT)
,#StartDate
,YEAR(#StartDate)
,MONTH(#StartDate)
SET #StartDate = DATEADD(d,1,#StartDate)
END;
GO
EXECUTE dbo.dateTest '2010-01-01'
SELECT * FROM dbo.dateDimension
The issue is that this logic:
DATEDIFF(day, #StartDate, GETDATE())
gives 3739 days with your current start date, so its never less than 30. Personally I would simply count it as follows:
DECLARE #StartDate DATETIME = '2010-01-01', #Count INT = 0;
WHILE #Count <= 30 BEGIN
INSERT into dbo.dateDimension
SELECT CAST( YEAR(#StartDate) * 10000 + MONTH(#StartDate) * 100 + DAY(#StartDate) AS INT)
, #StartDate
, YEAR(#StartDate)
, MONTH(#StartDate);
SET #StartDate = DATEADD(d,1,#StartDate);
set #Count = #Count + 1;
END;
SELECT *
FROM dbo.dateDimension;
If you are using SQL Server 2016 or above, this solution will not use a while loop, instead it uses a CTE to generate 30 rows numbered I to 30 and then uses the date to convert to yyyymmdd.
DECLARE #NUM_DAYS INT=30;
DECLARE #STARTDATE DATETIME='2020-01-01';
WITH CTE AS(
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS SRL
FROM STRING_SPLIT(REPLICATE(',',#num_days-1), ',') AS A
)
INSERT INTO dbo.dateDimension
SELECT
CONVERT(INT, CONVERT(CHAR(8), DATEADD(DAY, SRL-1, #STARTDATE), 112))
, #STARTDATE
, YEAR(#STARTDATE)
, MONTH(#STARTDATE)
FROM CTE
SELECT * FROM dbo.dateDimension

SQL Server - convert short date in from old database

I have old DBF database that contains date in old 6 digit formats like 291292 or 150793 or even 010302.
I import tables into SQL Server and now I need to convert it to datetime format.
Of course must be converted to similar strings 29.12.1992, 15.07.1993, 01.03.2002 first.
You may try this.
GO
declare #date date
set #date = '901124'
select CONVERT(varchar(10), #date, 102) as [Date] --- for yyyy.MM.dd format
TRY THIS
declare #dd varchar(10)
set #dd = '201292'
select CONVERT(VARCHAR(10), #dd, 2), CONVERT(VARCHAR(10), GETDATE(), 2)
, (SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2))
, CONVERT(varchar(10), cast((SUBSTRING(#dd, 5,2) + SUBSTRING(#dd, 3,2) + SUBSTRING(#dd, 1,2)) as DATE) , 102)
Ok it pretty simple:
GO
declare #date varchar(6)
set #date = '241190'
select CONVERT(date, concat(SUBSTRING(#date, 5,2),SUBSTRING(#date, 3,2),SUBSTRING(#date, 1,2)), 101) as [Date]
result is 1990-11-24
declare #date date = '901124'
select CONVERT(date, #date) as [Date]

Group data by interval of 15 minutes and use cross tab

I am trying to write a query based on datetime and weekday in sql server where my output should be like :
My table descriptions are:
**Branch**(DateKey integer,
BranchName varchar2(20),
TransactionDate datetime,
OrderCount integer)
**Date**(DateKey integer PrimaryKey,
DayNameofWeek varchar2(15))
This is the raw data I have
So, this is quite a long shot but I solved it the following way:
I created a table valued function, which would take a date as a parameter and find all 15-minute intervals during that day.
For each day it would go from 00:00, to 00:15, 00:30 up to 23:30, 23:45, and 23:59. It also returns each interval start time and end time, since we will need to use this for every row in your branch table to check if they fall into that time slot and if so, count it in.
This is the function:
create function dbo.getDate15MinIntervals(#date date)
returns #intervals table (
[interval] int not null,
[dayname] varchar(20) not null,
interval_start_time datetime not null,
interval_end_time datetime not null
)
as
begin
declare #starttime time = '00:00';
declare #endtime time = '23:59';
declare #date_start datetime;
declare #date_end datetime;
declare #min datetime;
select #date_start = cast(#date as datetime) + cast(#starttime as datetime), #date_end = cast(#date as datetime) + cast(#endtime as datetime);
declare #minutes table ([date] datetime)
insert into #minutes values (#date_start), (#date_end) -- begin, end of the day
select #min = DATEADD(mi, 0, #date_start)
while #min < #date_end
begin
select #min = DATEADD(mi, 1, #min)
insert into #minutes values (#min)
end
insert into #intervals
select ([row]-1)/15+1 intervalId, [dayname], min(interval_time) interval_start_time
> -- **NOTE: This line is the only thing you need to change:**
, DATEADD(ms, 59998, max(interval_time)) interval_end_time
from
(
select row_number() over(order by [date]) as [row], [date], datename(weekday, [date]) [dayname], [date] interval_time
from #minutes
) t
group by ([row]-1)/15+1, [dayname]
order by ([row]-1)/15+1
return
end
--example of calling it:
select * from dbo.getDate15MinIntervals('2017-07-14')
Then, I am querying your branch table (you don't really need the Date table, the weekday now you have it in the function but even if not, there's a DATENAME function in SQL Server, starting with 2008 that you can use.
I would query your table like this:
select branchname, [dayname], ISNULL([11:30], 0) as [11:30], ISNULL([11:45], 0) as [11:45], ISNULL([12:00], 0) as [12:00], ISNULL([12:45], 0) as [12:45]
from
(
select intervals.[dayname]
, b.branchname
, convert(varchar(5), intervals.interval_start_time, 108) interval_start_time -- for hh:mm format
, sum(b.ordercount) ordercount
from branch b cross apply dbo.getDate15MinIntervals(CAST(b.TransactionDate as date)) as intervals
where b.transactiondate between interval_start_time and interval_end_time
group by intervals.[dayname], b.branchname, intervals.interval_start_time, intervals.interval_end_time
) t
pivot ( sum(ordercount) for interval_start_time in ( [11:30], [11:45] , [12:00], [12:45] )) as p
Please note I have used in the PIVOT function only the intervals I can see in the image you posted, but of course you could write all 15-minute intervals of the day manually - you would just need to write them once in the pivot and once in the select statement - or optionally, generate this statement dynamically.

Calculating DATEDIFF(Month) in SQL Server

I am using the DateDiff function to find the number of months between two dates. But I am facing the problem like if the difference is 1 month and 3 days it is not giving as 2 months.
SELECT *
FROM tablename
WHERE
DATEDIFF(month, CONVERT(DATE, CONVERT(DATE, '20/10/2013', 103), 120),
CONVERT(DATE, CONVERT(DATE, '25/11/2013', 103), 120)) > 2
Result should be 2 but it is giving 1
The date column in the table is varchar type values are stored as dd/mm/yyyy. Even one day in the month should be calculated as 1 month
Got the Answer
ALTER FUNCTION [dbo].[CalculateNumberOfMonths]
(
#date1 varchar(50),
#date2 varchar(50)
)
RETURNS varchar(10)
AS
BEGIN
declare #days int
declare #months int
select #days = datediff(day,CONVERT(DATE,#date1,103),CONVERT(DATE,#date2,103))
select #months = datediff(month,CONVERT(DATE,#date1,103),CONVERT(DATE,#date2,103))
if(#days % 7 >0)
begin
set #months= #months + 1
end
RETURN #months
END

sql query calculating no of employees joined each financial year i.e from 1-04-2002 to 31-03-2003

I have a table in which joining dates are give in datetime format.
I have to calculate how many employees joined each financial year resp. ie for eg from
1-04-2002 to 31-03-2003.this should work for each year..from 2003 to 2004,2004 to 2005...n so on.
can anybdy help?
thanxx.
You can map start date to financial year using YEAR(DATEADD(M,-3,JoinDate) I think and you can count records with a CTE, e.g.
with EmployeeStartFinYear(FinYear, EmployeeId)
as
(
select year(dateadd(M,-3,JoinDate)), EmployeeId
from Employees
where JoinDate is not null
)
select FinYear, count(EmployeeId)
from EmployeeStartFinYear
group by FinYear
order by FinYear;
Here's my answer. I think it looks horrid, but i think it works. I'll explain the logic behind it.
I declared the Start and End dates just because it's easier to write than a date.
The #Years variable is the difference in years between the start and end date.
#Counter is used to loop through the number of years stored in #Years. #Diff is always one more than #Counter because each time we go through the loop, we want to increment the date range so it's always 1 year, rather than be counting employees that joined in 1 year, then 2 years etc.
#TempTable stores the info we get each time we go through the query.
All the query does is get the count of employees between the Start Date and a year from that start date and puts it into a temp table. Then it looks through again, and gets the employees that started between Start Date + 1 and Start Date + 2.
Sorry if it's horrible and ugly and doesn't work.
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
DECLARE #Years TINYINT
DECLARE #Counter TINYINT
DECLARE #Diff TINYINT
DECLARE #TempTable TABLE
(
FinancialYear VARCHAR(9)
,Employees TINYINT
)
SET #Count = 0
SET #Diff = 1
SET #Years = DATEDIFF(yyyy, #StartDate, #EndDate)
WHILE #Count < #Years - 1
BEGIN
SELECT
CAST(DATEPART(yyyy, DATEADD(yyyy, #Count, #StartDate) AS VARCHAR(4)) + '-' + CAST(DATEPART(yyyy, DATEADD(yyyy, #Diff, #StartDate)) AS VARCHAR(4) AS FinancialYear
,COUNT(employee_id) AS Employees
INTO #TempTable
FROM
Employees
WHERE
join_date >= #StartDate AND join_date < DATEADD(yyyy, 1, #StartDate)
GROUP BY
CAST(DATEPART(yyyy, DATEADD(yyyy, #Count, #StartDate) AS VARCHAR(4)) + '-' + CAST(DATEPART(yyyy, DATEADD(yyyy, #Diff, #StartDate)) AS VARCHAR(4)
SET #Count = #Count + 1
SET #Diff = #Diff + 1
END
SELECT * FROM #TempTable

Resources