Get everyday of the week in SQL - sql

i am trying to write a sql query for getting the every day of the week.
like if i run SELECT GETDATE() i am getting today'day date if run the getdate()-1 i am getting only column with yesterday date. Instead i want to get the every day in a week in different columns, someone please help with the SQL. Any help will be appreciated.

try this:
SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) Monday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 1 Tuesday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 2 Wednsday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 3 Thursday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 4 Friday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 5 Saturday,
DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) + 6 Sunday;

Does this work?
SELECT
Day1 = CAST(getdate() AS date),
Day2 = CAST(getdate()+1 AS date),
Day3 = CAST(getdate()+2 AS date),
Day4 = CAST(getdate()+3 AS date),
Day5 = CAST(getdate()+4 AS date),
Day6 = CAST(getdate()+5 AS date),
Day7 = CAST(getdate()+6 AS date);
Returns:
Day1 Day2 Day3 Day4 Day5 Day6 Day7
---------- ---------- ---------- ---------- ---------- ---------- ----------
2017-06-26 2017-06-27 2017-06-28 2017-06-29 2017-06-30 2017-07-01 2017-07-02
If this is not what you are looking for can you post what you want your query to return?

Related

Rounding of hours in sql queries

The values ​​you see below are loaded with a query and they are related to a time stamp. What they ask me to insert is a rounded value; up and down. Rounding must be done at minutes of 15 and 30.
If the round is set to 15 and the marking has been made at 7:59, it is rounded off to 8:00 if the marking is at 8:01 am rounded to 8:15 am, as do I implement this thing within this query?
Query:
select Data, string_agg(Ore, ' ') as Ore
from (
select FORMAT(DataCreazione, 'dd/MM/yyyy', 'it-IT') as Data,
CONCAT(DATEPART(HOUR,DataCreazione), ':', DATEPART(MINUTE, DataCreazione)) as
Ore
from Marcatura
where IdUtente = 2
and (Stato='Ingresso' or Stato='Uscita')
and cast(DataCreazione as DateTime)
between cast(CONVERT(VARCHAR(10), CONVERT(date, '10-11-18', 5), 23) as datetime)
and cast(CONVERT(VARCHAR(10), CONVERT(date, '10-11-19', 5), 23) as datetime)
) t
group by Data
order by CONVERT(datetime, Data, 105) desc
Values:
05/07/2019 -- 14:45 19:27
04/07/2019 -- 11:41 11:41
07/06/2019 -- 12:39
01/06/2019 -- 8:27 8:27 8:27 8:27
18/04/2019 -- 15:41 15:41
08/04/2019 -- 11:52 11:54
01/04/2019 -- 7:25
27/03/2019 -- 21:38 21:38
23/03/2019 -- 13:32 13:32
08/03/2019 -- 21:20 21:20
04/03/2019 -- 21:48 21:48
02/03/2019 -- 8:3 8:3
If the state is equal to < Ingresso > it is rounded up to the top if the status is < Uscita > is rounded down, for example 07:59 becomes 08:00 whereas if it is 17:44 it becomes 17:45
I would use timefromparts():
select dateadd(minute,
(case when datepart(minute, #time) not in (0, 15, 30, 45) then 15 else 0 end),
timefromparts(datepart(hour, #time),
15 * floor(datepart(minute, #time) / 15.0) % 60,
0, 0, 0
)
)
This is a little more complicated because you want to round up. So the idea is to round down and then selectively add 15 minutes.
The Logic is much more simple than described, perhaps.
Try this approach:
EDIT: Applied logic to your own query
select Data, string_agg(Ore, ' ') as Ore
from (
select FORMAT(DataCreazione, 'dd/MM/yyyy', 'it-IT') as Data,
CONCAT(
(Case
when Datepart(MINUTE, DataCreazione) > 45
then Datepart(hour, Datacreazione)+1
else Datepart(hour, Datacreazione)
end)
,':'
,(case
when DATEPART(MINUTE, DataCreazione) between 0 and 15 then '15'
when DATEPART(MINUTE, DataCreazione) between 16 and 30 then '30'
when DATEPART(MINUTE, DataCreazione) between 31 and 45 then '45'
else '00' end
)) as Ore
from Marcatura
where IdUtente = 2
and (Stato='Ingresso' or Stato='Uscita')
and DataCreazione
between cast(CONVERT(VARCHAR(10), CONVERT(date, '10-11-18', 5), 23) as datetime)
and cast(CONVERT(VARCHAR(10), CONVERT(date, '10-11-19', 5), 23) as datetime)
) t
group by Data
order by CONVERT(datetime, Data, 105) desc;
SQLFiddle here:
http://sqlfiddle.com/#!18/475202/1
http://sqlfiddle.com/#!18/23d16/13
this logic might help.
declare #time time
set #time = '14:41'
select case
when datepart(mi, #time) between 15 and 30 then dateadd(mi, -datepart(mi, #time) + 30, #time)
when datepart(mi, #time) between 16 and 45 then dateadd(mi, -datepart(mi, #time) + 45, #time)
when datepart(mi, #time) between 46 and 59 then dateadd(mi, -datepart(mi, #time) + 60, #time)
else dateadd(mi, -datepart(mi, #time), #time) end
applying this in your query.
select Data, string_agg(Ore, ' ') as Ore
from (
select FORMAT(DataCreazione, 'dd/MM/yyyy', 'it-IT') as Data,
CONVERT(VARCHAR(5), (case
when datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108)) between 15 and 30 then dateadd(mi, -datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108))) + 30, CONVERT(VARCHAR(5),DataCreazione,108)))
when datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108))) between 16 and 45 then dateadd(mi, -datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108))) + 45, CONVERT(VARCHAR(5),DataCreazione,108)))
when datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108))) between 46 and 59 then dateadd(mi, -datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108))) + 60, CONVERT(VARCHAR(5),DataCreazione,108)))
else dateadd(mi, -datepart(mi, CONVERT(VARCHAR(5),DataCreazione,108))), CONVERT(VARCHAR(5),DataCreazione,108))) end), 108) as Ore
from Marcatura
where IdUtente = 2
and (Stato='Ingresso' or Stato='Uscita')
and cast(DataCreazione as DateTime)
between cast(CONVERT(VARCHAR(10), CONVERT(date, '10-11-18', 5), 23) as datetime)
and cast(CONVERT(VARCHAR(10), CONVERT(date, '10-11-19', 5), 23) as datetime)
) t
group by Data
order by CONVERT(datetime, Data, 105) desc

SQL QUERY FROM 25(LAST MONTH) to 25(CURRENT MONTH)

I would like a query that would include from day 25 of last month to 25 of current month. Example: 25/12/2017-25/01/2018
I can't do something like transaction_date >= getdate()-31 because there are months that have 28,29 days.
Thanks!
You could use:
SELECT *
FROM your_tab
WHERE transaction_date
BETWEEN DATEFROMPARTS(IIF(MONTH(GETDATE()) = 1,YEAR(GETDATE())-1,YEAR(GETDATE())),
IIF(MONTH(GETDATE()) = 1, 12 , MONTH(GETDATE())-1)
,25)
AND DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 25);
One easy way
where
transaction_date between dateadd(dd, 25, eomonth(getdate(),-2))
and dateadd(dd, 25, eomonth(getdate(),-1))
I suggest DATEFROMPARTS to make a date with a set day, and subtract 1 month from the beginning:
WHERE [transaction_date] BETWEEN DATEADD(month, -1, DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 25)) AND DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 25)
You can use datetime functions to substract a month from a date:
-- Feb 2016 is 29 days
select dateadd(month, -1, '2016-01-25 00:00')
>> 2015-12-25 00:00:00.000
select dateadd(month, -1, '2016-02-25 00:00')
>> 2016-01-25 00:00:00.000
select dateadd(month, -1, '2016-03-25 00:00')
>> 2016-02-25 00:00:00.000
-- Feb 2017 is 28 days
select dateadd(month, -1, '2017-01-25 00:00')
>> 2016-12-25 00:00:00.000
select dateadd(month, -1, '2017-02-25 00:00')
>> 2017-01-25 00:00:00.000
select dateadd(month, -1, '2017-03-25 00:00')
>> 2017-02-25 00:00:00.000
declare #thatDate datetime = '2017-01-25 00:00'
select *
from ThisTable
where ThisDate between dateadd(month, -1, #thatDate) and #thatDate
If you are on mysql this might work:
SELECT *
FROM SOME_TABLE
WHERE transaction_date
BETWEEN
DATE_FORMAT(NOW(), '%Y-%m-25') - INTERVAL 1 MONTH
AND
DATE_FORMAT(NOW(), '%Y-%m-25')
You could do something along the lines of:
Select fields from table
where whateverDate > '25/12/2017' and
whateverDate < '25/01/2018'

Filtering data from the previous day using DateTime values

I am trying to retrieve data for yesterday's shift using SQL Server 2008 r2.
The shift starts at 20:00 pm, and ends 10:00 am following day.
What date function can I use to retrieve data for the entire shift?
Using this:
CAST([LastStartedDate] as time)> cast('20:00' as Time)
Will only retrieve data up to midnight.
You can filter your dates with a WHERE clause that uses some date manipulation like so:
WHERE LastStartedDate >= DATEADD(HOUR, 20,
CAST(CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS DATETIME));
This first gets yesterdays date, as a DATE so it removes the time portion:
SELECT CAST(DATEADD(DAY, -1, GETDATE()) AS DATE)
-- 2017-03-06
Then converts it back to a DATETIME to add the time as midnight of that day:
SELECT CAST(CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS DATETIME)
-- 2017-03-06 00:00:00.000
Then it adds 20 hours to get you to 20:00 - 8.00PM:
SELECT DATEADD(HOUR, 20,CAST(CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS DATETIME));
-- 2017-03-06 20:00:00.000
For your scenario, you need to do the same again to get the 10am cut off too and use BETWEEN.
Example:
CREATE TABLE #shift
(
LastStartedDate DATETIME ,
WorkItemsDone INT
);
INSERT INTO #shift
( LastStartedDate, WorkItemsDone )
VALUES ( DATEADD(HOUR, -18, GETDATE()), 10 ),
( DATEADD(HOUR, -15, GETDATE()), 20 ),
( DATEADD(HOUR, -14, GETDATE()), 30 ),
( DATEADD(HOUR, -10, GETDATE()), 40 ),
( DATEADD(HOUR, -5, GETDATE()), 25 ),
( DATEADD(HOUR, -2, GETDATE()), 15 ),
( DATEADD(HOUR, 4, GETDATE()), 5 ),
( DATEADD(HOUR, 10, GETDATE()), 15 );
-- all data
SELECT *
FROM #shift
-- limited data
SELECT *
FROM #shift
WHERE LastStartedDate BETWEEN
DATEADD(HOUR, 20, CAST(CAST(DATEADD(DAY, -1, GETDATE()) AS DATE) AS DATETIME))
AND DATEADD(HOUR, 10, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
DROP TABLE #shift;
Produces:
-- all data
LastStartedDate WorkItemsDone
2017-03-06 16:18:04.877 10
2017-03-06 19:18:04.877 20
2017-03-06 20:18:04.877 30
2017-03-07 00:18:04.877 40
2017-03-07 05:18:04.877 25
2017-03-07 08:18:04.877 15
2017-03-07 14:18:04.877 5
2017-03-07 20:18:04.877 15
-- filtered data
LastStartedDate WorkItemsDone
2017-03-06 20:18:04.877 30
2017-03-07 00:18:04.877 40
2017-03-07 05:18:04.877 25
2017-03-07 08:18:04.877 15

How to get firstdate and lastdate of eachmonth

Using Sql Server 2000
Table1
id sdate edate
001 05/01/2012 25/02/2012
002 19/02/2012 17/04/2012
.....
sDate, eDate format is dd/mm/yyyy
I want to get firstdate and lastdate of eachmonth from the table1 like 01/01/2012 31/01/2012, 01/02/2012 29/02/2012
Expected Output
id sdate edate
001 05/01/2012 31/01/2012
001 01/02/2012 25/02/2012
002 19/02/2012 29/02/2012
002 01/03/2012 31/03/2012
002 01/04/2012 17/04/2012
.....
How to make a query for the above condition?
Need Query help
SELECT
DATEADD(mm, DATEDIFF(mm, 0, sdate), 0),
DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, edate) + 1, 0))
from yourtable;
Its actually a two part query:
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0);
SELECT DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0));
Above will give you start date and end date for the current month,

find records from previous x days?

How can I come up with a stored procedure that selects results for past 30 days?
where MONTH(RequestDate) > 6 and DAY(RequestDate) >= 10
and MONTH(RequestDate) < 21 and DAY(RequestDate) < 7
SELECT *
FROM Table
WHERE GETDATE() >= DATEADD(DAY, -30, GETDATE())
Substitute the first GETDATE() with the appropriate column name.
SELECT *
FROM Table
WHERE Table.ColumnName >= DATEADD(DAY, -30, GETDATE())
Are you looking for last 30 days or last month? To find start and end of each month ("generic" as your comment says), use:
select dateadd(month,datediff(month,0,getdate()),0),
dateadd(mm,datediff(mm,-1,getdate()),-1)
Something like this?
CREATE PROC GetSomeHistory
#NumDaysPrevious int
AS
BEGIN
SELECT * FROM MyTable
WHERE RequestDate BETWEEN DATEADD(dd, -1 * #NumDaysPrevious, getdate())
AND getdate();
END
......
EXEC GetSomeHistory 55;
SELECT *
FROM Table
WHERE myDate >= DATEADD(MONTH, -1, GETDATE())
doing it by month is different than doing it in 30-day blocks. Test this out as follows...
declare #mydate smalldatetime
set #mydate = '07/6/01'
select #mydate
select DATEADD(month, 2, #mydate), DATEDIFF(day, DATEADD(month, 2, #mydate), #mydate)
select DATEADD(month, 1, #mydate), DATEDIFF(day, DATEADD(month, 1, #mydate), #mydate)
select DATEADD(month, -1, #mydate), DATEDIFF(day, DATEADD(month, -1, #mydate), #mydate)
select DATEADD(month, -2, #mydate), DATEDIFF(day, DATEADD(month, -2, #mydate), #mydate)
select DATEADD(month, -3, #mydate), DATEDIFF(day, DATEADD(month, -3, #mydate), #mydate)
Here are the results:
2001-07-06 00:00:00
2001-09-06 00:00:00 | -62
2001-08-06 00:00:00 | -31
2001-06-06 00:00:00 | 30
2001-06-06 00:00:00 | 30
2001-05-06 00:00:00 | 61
2001-04-06 00:00:00 | 91