I’m trying to convert the below to MariaDB in order to get the next relevant date from today’s date.
The below SQL will get the next Sunday from the specified date
DECLARE #NextDayID INT;
SET #NextDayID = 1; -- Next Sunday
SELECT DATEADD(DAY, (DATEDIFF(DAY, ((#NextDayID + 5) % 7), GETDATE()) / 7) * 7 + 7, ((#NextDayID + 5) % 7)) AS NextDay
You can use date arithmetic:
select curdate() + interval (7 - dayofweek(curdate()) + 1) % 7 day
Related
Is it possible to random only days in date or Datetime within a range of days in sql server, t-sql.
For example if dates have days between 1to15 i want to randomise days creating a day number between 16 and 30. Example: 2017-7-10 to 2017-7-21(random day within 16to30). Thank you
DECLARE #dateA DATE = '2017-07-10'
DECLARE #dateB DATE
IF day(#dateA) < 16 --day number between 1 and 15
SET #dateB = DATEFROMPARTS(year(#dateA), month(#dateA), FLOOR(RAND() * (30 - 16) + 16))
SELECT #dateA, #dateb
DATEFROMPARTE SQL Server 2012+
No version specific:
DECLARE #dateA DATE = '2017-07-10'
DECLARE #dateB DATE
DECLARE #day INT
IF day(#dateA) < 16 --day number between 1 and 15
BEGIN
SET #day = day(#dateA)
SET #dateB = dateadd(day, - 1 * #day + FLOOR(RAND() * (30 - 16) + 16), #dateA)
END
SELECT #dateA, #dateb
New request:
UPDATE yourTable
SET yourDateOrDatetimeColumn = dateadd(day, - 1 * day(yourDateOrDatetimeColumn) + FLOOR(RAND() * (30 - 16) + 16), yourDateOrDatetimeColumn)
WHERE day(yourDateOrDatetimeColumn) < 16
SELECT RandomDate16To30DaysFromToday = DATEADD(DAY, FLOOR(RAND()*(30-16)+16), GETDATE())
I am using the query below to get the number of the week in a month:
datepart(day, datediff(day, 0,TABLE.DATE), 102))/7 * 7)/7 + 1
Now I need to change the script that if a week ends in the next month, it should show 1 instead of 5.
Does anybody can help me?
Using Modulo by 4, you can get the month number 1 instead of 5.
For an example I tested with the 5th week and it result as 1:
DECLARE #TestDate AS DATETIME = '2016-07-29 10:00:00';
SELECT CASE ((DATEPART(DAY, DATEDIFF(DAY, 0, #TestDate)) /7 * 7) /7 + 1) % 4
WHEN 0 THEN 4
ELSE ((DATEPART(DAY, DATEDIFF(DAY, 0, #TestDate)) /7 * 7) /7 + 1) % 4 END
In your query, you wrongly used DATEPART. Actually it requires 2 parameters only.
I have a Execute SQL task in SSIS, which updates date in Database to today's date+3. But if suppose today is Thursday say(18/6/2015) and jobs executes then according to update query date in Database will be update to (21/6/2015) out of which 20th and 21st are Sat and Sunday. Instead i would want the date to be updated to 23/6/2015 i.e Tuesday( excluding Sat and Sunday). Please help!
I've made the following SELECT statement which may help you.
Using the DATEPART function you can discover which day of the week a sum of days will be.
DATEPART returns a number representing the day of the week of the date passed as parameter to the function, considering 1 is Sunday and 7 is Saturday.
select CASE
WHEN DATEPART(DW, GETDATE() + 4) = 7 THEN (SELECT GETDATE() + 6)
WHEN DATEPART(DW, GETDATE() + 5) = 1 THEN (SELECT GETDATE() + 5)
END As date
Using this example in an update statement:
UPDATE <your table>
SET dateofanything = (CASE
WHEN DATEPART(DW, GETDATE() + 3) = 7 THEN (SELECT GETDATE() + 5)
WHEN DATEPART(DW, GETDATE() + 3) = 1 THEN (SELECT GETDATE() + 4)
END As date)
declare #date varchar(30) = '2013-04-18'
DECLARE #WeekOfMonth TINYINT
SET #WeekOfMonth =
(
DAY(#DATE) +
(DATEPART(dw,
DATEADD (MONTH,
DATEDIFF (MONTH,
0,
#DATE),
0))
- 1) -1) / 7 + 1
print #WeekOfMonth
This is a query I found over the internet to find out week number of the month like it's '2013-03-04' today and this is second week of this month, Query is working fine, But I can't understand it and the person who has posted it over that blog haven't described it's working either. Can someone please help me to understand it better.
It's calculating the week number of the month, by adding the day number to the weekday number of the first day of that month, then divide by 7 and add 1. For example, with '2013-03-04', the day number is 4, the week number of '2013-03-01' is 6 (Friday), so the result is (4 + 6 - 1 - 1)/7 + 1 = 2.
Working from the inside out:
(DAY('2013-03-04') + (DATEPART(dw, DATEADD(MONTH, DATEDIFF(MONTH, 0, '2013-03-04'), 0)) - 1) - 1) / 7 + 1
Number of months from 1900-01-01, 1358
(DAY('2013-03-04') + (DATEPART(dw, DATEADD(MONTH, 1358, 0)) - 1) - 1) / 7 + 1
The date that is 1358 months from 1900-01-01, 2013-03-01
(DAY('2013-03-04') + (DATEPART(dw, '2013-03-01') - 1) - 1) / 7 + 1
Day of week of '2013-03-01', 6 (Friday)
(DAY('2013-03-04') + (6 - 1) - 1) / 7 + 1
The day part of '2013-03-04', 4
(4 + (6 - 1) - 1) / 7 + 1
-1 to convert Sunday = day 1 to Monday = day 1
(4 + 5 - 1) / 7 + 1
-1 because '2013-03-04' is 3 days from '2013-03-01'
= 2
Here is another way of looking at this:
You can run these statements to break it down yourself:
select datediff(month, 0, getdate()); -- Get Months since 1900-01-01
select DATEADD (MONTH, DATEDIFF (MONTH, 0, getdate()),0); -- Add Months back to 1900-01-01 to get 1st of Month (essentially stripping time and days)
select DATEPART(dw,DATEADD (MONTH, DATEDIFF (MONTH, 0, getdate()),0)-1); Get Day of Week Number for 1st of Current Month
select (day(getdate()) + DATEPART(dw,DATEADD (MONTH, DATEDIFF (MONTH, 0, getdate()),0)-1)-1); -- Add Day Number of Month for Current Date then subtract 1 to make it days, since we started on 1st
select (day(getdate()) + DATEPART(dw,DATEADD (MONTH, DATEDIFF (MONTH, 0, getdate()),0)-1)-1)/7; -- Determine how many whole weeks can be divided into this result
select (day(getdate()) + (DATEPART(dw,DATEADD (MONTH, DATEDIFF (MONTH, 0, getdate()),0))-1)-1)/7+1; -- We're zero indexed (meaning results would be zero in 1st week), so add 1 to get week number
I have been tasked with writting a report from MS Dynamic CRM. The report contains appointment and account information. I am using Visual Studio 2005 and SQL 2005.
The problem I have found is that if an appointment has a scheduled start date that falls after Britsh Summer Time but has a creation date before (BST) then the ScheduledStart field of the dbo.Appointment DB udates with the offset time + or - on hour. This means when I look for an appointment using reporting services and key in the time the appointment is due to start it returns no results because in the DB the time is either one hour earlier or later. I can manually amend the time for my search but this then returns the incorrect time on the report.
This will of course ony happen twice a year as appointments are only arranged a couple of weeks in advance but it is still a pain!
Is there a way using Transact SQL (or any method availbale to SSRS) I can allow for daylight saving times so even though the DB shows a Sceduled Start (dbo.Appointment.ScheduledStart) of say 11:00:00 the appointment is actually due to start at 10:00:00?
Alternately this case statement will work inside your existing query.
SELECT CASE
WHEN ([Created_Date] BETWEEN
Dateadd(yy, Datediff(yy, 0, [Scheduled_Date]), 0)
AND
Convert(DATETIME, Convert(VARCHAR(4), Year([Created_Date])) + '-03-' + Convert(VARCHAR(2), (31 - (5 * Year([Created_Date])/4 + 4) % 7)) + ' 01:00:00', 20))
AND
([ScheduledDate] BETWEEN
Convert(DATETIME, Convert(VARCHAR(4), Year([Created_Date])) + '-03-' + Convert(VARCHAR(2), (31 - (5 * Year([Created_Date])/4 + 4) % 7)) + ' 01:00:00', 20)
AND
Convert(DATETIME, Convert(VARCHAR(4), Year([Created_Date])) + '-10-' + Convert(VARCHAR(2), (31 - (5 * Year([Created_Date])/4 + 1) % 7)) + ' 00:00:00', 20))
THEN Dateadd(hh, 1, [Scheduled_Date])
ELSE [Scheduled_Date]
END AS [Scheduled_Date]
CREATE FUNCTION OffsetBST (#CreatedDateTime DATETIME,
#ScheduledDateTime DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE #InDateYear VARCHAR(4)
DECLARE #StartDay VARCHAR(2)
DECLARE #EndDay VARCHAR(2)
DECLARE #BSTStart DATETIME
DECLARE #BSTEnd DATETIME
DECLARE #OffsetDateTime DATETIME
SET #StartDay = Convert(VARCHAR(2), (31 - (5 * Year(#CreatedDateTime)/4 + 4) % 7))
SET #EndDay = Convert(VARCHAR(2), (31 - (5 * Year(#CreatedDateTime)/4 + 1) % 7))
SET #InDateYear = Convert(VARCHAR(4), Year(#CreatedDateTime))
SET #BSTStart = Convert(DATETIME, #InDateYear + '-03-' + #StartDay + ' 01:00:00', 20)
SET #BSTEnd = Convert(DATETIME, #InDateYear + '-10-' + #EndDay + ' 00:00:00', 20)
IF (#CreatedDateTime BETWEEN DATEADD(yy, DATEDIFF(yy,0,#ScheduledDateTime), 0) AND #BSTStart)
AND (#ScheduledDateTime BETWEEN #BSTStart AND #BSTEnd)
SET #OffsetDateTime = Dateadd(hh, 1, #ScheduledDateTime)
ELSE
SET #OffsetDateTime = Dateadd(hh, 0, #ScheduledDateTime)
RETURN #OffsetDateTime
END
You can use this in your data source select statement. You will provide it with the created date and scheduled date and it will determine if the scheduled date requires a BST offset.
Like so:
SELECT A.Appointment_ID, A.Appointment_Name, dbo.OffsetBST(A.Created_Date,
A.Scheduled_Date) as Scheduled_Date
FROM AppointmentsTable as A