Error with SQL CONVERT GETDATE() for leap years - sql-server

What I'm trying to do here is add a time component to GETDATE() since it's being converted to varchar(25). This is my statement, how would I do it?
CONVERT(Varchar(25),YEAR(GETDATE())-1)
Would it be something along CONVERT(Varchar(25),year(getDate()) -1)
This CONVERT is actually a part of:
DATEADD(m, 6,CAST(CONVERT(Varchar(25),MONTH(tblDateApt. Date)) +
'/' + CONVERT(Varchar(25),DAY(tblDateApt. Date))
+ '/' + CONVERT(Varchar(25),YEAR(GETDATE())-1) As DateTime))
The problem is when I run this statement on a leap year date I get an error. I'm trying to add a time to getDate before it gets casted as DATETIME
EDIT 2
I'm simply trying to make this give return a value...
select DATEADD(m, 6,CAST(CONVERT(Varchar(25),MONTH('2/29/2016')) + '/' + CONVERT(Varchar(25),DAY('2/29/2016')) + '/' + CONVERT(Varchar(25),YEAR(GETDATE())-1) As DateTime))

Breaking the date into strings and rebuild it into date is almost never the correct solution.
Assuming I understand the problem, you want to get the dates from your database, and manipulate the year part to be a year before the current year.
Try this:
SELECT tblDateApt.[Date],
DATEADD(Month,
6,
DATEADD(YEAR,
YEAR(GETDATE()) - 1 - YEAR(tblDateApt.[Date]),
tblDateApt.[Date])
)
FROM tblDateApt
Edited to get the date of 6 months after the date in the database after manipulating it to last year.

This will leave you with DateTime value taken from tblDateApt.Date decreased by one year and increased by 6 months (as per your intent):
SELECT DATEADD(month,
6,
DATEADD(year,
YEAR(GETDATE()) - YEAR(tblDateApt.date) - 1,
tblDateApt.date
)
)
Avoid any conversions to and from text content.

Related

Format function is not working at where clause to filter the format date?

Getting the months list of current year and Previous year
SELECT FORMAT(DATEADD(month,number,CAST(YEAR(DATEADD(YEAR,-1,GETDATE())) AS varchar(4)) + '-01-01'),'MMM') + '-' +
SUBSTRING(CAST(CAST(YEAR(GETDATE()) AS VARCHAR(4)) + IIF(number<12,-1,0) AS VARCHAR(4)),3,2) as Months into #temptable
FROM master..spt_values
WHERE type = 'P'
AND number < 24
Expected Output:-1
From Current Month- Mar-19
Feb-19
End of CurrentYear Month -Jan-19
ExpectedOutPut:2
Jan-18
to
Dec -18
I'm trying this query
select *from #temptable where Months>=FORMAT(DATEADD(yy, DATEDIFF(yy, 0, getdate()), 0),'MMM-yy') and Months<=FORMAT(DATEADD(mm, DATEDIFF(mm, 0, getdate()), 0),'MMM-yy')
select *from #temptable where Months>=FORMAT(DATEADD(yy, DATEDIFF(yy, 0, getdate())-1, 0),'MMM-yy') and Months<= FORMAT(DATEADD(yy, DATEDIFF(yy, 0, getdate()), -1),'MMM-yy')
Seeing as you are essentially storing your month/ years as text, to actually compare them to dates is painful at best. You would be much better storing either real dates, or years/ months as two separate values, maybe with a text version as well for display purposes?
Anyway, based on your poor data model you could do this to get what you need:
SELECT * FROM #temptable WHERE YEAR(CONVERT(DATE, '01-' + Months)) = YEAR(GETDATE()) - 1;
SELECT * FROM #temptable WHERE YEAR(CONVERT(DATE, '01-' + Months)) = YEAR(GETDATE()) AND MONTH(CONVERT(DATE, '01-' + Months)) <= MONTH(GETDATE());
But note that this now introduces a dependency on your regional format, as this wouldn't work in America (for example) as day comes after month. You would be far better using ANSI date format of YYYYMMDD (or storing the dates as year/ months, or hey(!) even as dates).

SSIS expression previous date without DateAdd()

Currently developing a package that passes an expression from a previous date to a filename. The current code I have is the following as a string variable:
(DT_WSTR,20)DATEPART("YYYY",Dateadd("DD",-1,dateadd("MM",datediff("MM",
(DT_DATE) "1900-01-01",getdate())-2, (DT_DATE) "1900-01-01")))
+ RIGHT("0"+(DT_WSTR,20)DATEPART("MM",Dateadd("DD",-1,dateadd("MM",datediff("MM", (DT_DATE) "1900-01-01",getdate())-5, (DT_DATE) "1900-01-01"))),2)
+ "01"
This currently produces the output of:
20171101
This is currently incorrect because I'd like the date to be from the previous year:
20161101
Here's the forumula I'd like:
Return the 1st day of the month that is 7 months in the past from today's date.
Example: 5/2/2017 would return 11/1/2017; 6/21/2017 would return 12/1/2016; 7/10/2017 would return 1/1/2017; etc.
Is this possible to do via a variable in SSIS?
Your expression can be modified (and simplified) to this
(DT_WSTR, 8)( ( YEAR( DATEADD( "MM", -7, GETDATE() ) ) * 10000 ) + ( MONTH( DATEADD("MM", -7, GETDATE() ) ) * 100 ) + 1 )
subtract 7 months from current date
multiply resulting year by 10000
subtract 7 months from current date
multiply resulting month by 100
add year-value, month-value and 1 (first day)
convert to string
Credit to #Rangani in Yesterday's date in SSIS package setting in variable through expression for "multiply and add instead of string concat" trick
SELECT LEFT(CONVERT(VARCHAR, DATEADD("MM", -6, '2017-05-02'), 112), 6) + '01'
SELECT LEFT(CONVERT(VARCHAR, DATEADD("MM", -6, '2017-06-21'), 112), 6) + '01'
SELECT LEFT(CONVERT(VARCHAR, DATEADD("MM", -6, '2017-07-10'), 112), 6) + '01'

TSQL query for Week Comparison

I have a table that has a TASK_START_DATE and TASK_FINISH_DATE Columns of type datetime
I need help with a query that returns all Tasks when the Task: (date = just the date - I think I can do a conversion to the date from datetime on SQL 2008R2, it works fine)
- is within 2 weeks previous of the current date or two weeks after the current date.
Similarly I also need the records whose TaskEnd values are within 2 weeks previous or two weeks before
I've been trying things like which would get tasks where the start date is within the two previous weeks, but I have to do the same for TASK_FINISH_DATE and I think my and's and or's are all jumbled up, any help is appreciated.
Convert(Date, TASK_START_DATE) <= Convert(Date, DateAdd(ww, -2, GetDate()))
Short version:
How do I correctly write a query that combines all records with the TASK_START_DATE OR TASK_END_DATE within two weeks in the future or past, i.e.
Select Task_ID, TASK_NAME, TASK_START_DATE, TASK_END_DATE
where
???
You can add days to your date for comparision:
Select * from Table
Where column between getdate()-14 and getdate()+14
You don't need to use "Convert" function. "GetDate" function returns datetime value and your columns' types are datetime. You can add day number directly like this:
Select * from Table
Where (TASK_START_DATE between getdate() - 14 and getdate() + 14)
or (TASK_FINISH_DATE between getdate() - 14 and getdate() + 14)
You can declare variables or have the comparison dates right in the where clause. I use GETDATE() to get the date/time for right now as it returns a DATETIME object. Then I use DATEADD to adjust it for days, months, years, etc, and then you have to convert it to a DATE before sticking it in a variable of type DATE. Note in the DATEADD method I pass in the adjustment type (D = days), then adjust it + or - 14 days.
Alternatively you could just use 14 days ago to the minute if you don't do the DATE conversions...you'd have to remove the converts from the variable declarations as well as the where clause. Depends on the results you want though.
DECLARE #twoWeeksAgo DATE = CONVERT(DATE, DATEADD(D, -14, GETDATE()));
DECLARE #twoWeeksAhead DATE = CONVERT(DATE, DATEADD(D, 14, GETDATE()));
SELECT
Task_ID,
TASK_NAME,
TASK_START_DATE,
TASK_END_DATE
FROM
TABLE
WHERE
CONVERT(DATE, TASK_START_DATE) BETWEEN #twoWeeksAgo AND #twoWeeksAhead
OR CONVERT(DATE, TASK_END_DATE) BETWEEN #twoWeeksAgo AND #twoWeeksAhead
Also note that the BETWEEN operator in the WHERE clause is inclusive, meaning it will include records where the TASK_START_DATE is equal to the dates held by the variables. If you wanted to exclude records with the same value as #twoWeeksAhead, for example, you would have to use something like
WHERE
(CONVERT(DATE, TASK_START_DATE) >= #twoWeeksAgo
AND CONVERT(DATE, TASK_START_DATE) < #twoWeeksAhead)
OR (CONVERT(DATE, TASK_END_DATE) >= #twoWeeksAgo
AND CONVERT(DATE, TASK_END_DATE) < #twoWeeksAhead)

SQL to select records with a date greater than 40 days prior to start of month

I have a field in my table that holds a date and is in Datetime format.
I wish to select all records where the date in that field is greater than the date that is 40 days prior to the 1st of the current month.
Struggling to come up with the correct T-SQL syntax to do this.
This expression gives you the date you are looking for:
DATEADD(DAY, -DATEPART(d, GETDATE()) + 1 - 40, GETDATE())
Will this work
select #urdatecolumn > (getdate()- (DAY(getdate()) + 40-1))
Try this
where dt_blah > DATEADD(d,-40,DATEADD(wk, DATEDIFF(wk, 0, dateadd(dd, 6 - datepart(day, dt_blah), dt_blah)), 0)

Deterministic scalar function to get day of week for a date

SQL Server, trying to get day of week via a deterministic UDF.
Im sure this must be possible, but cant figure it out.
UPDATE: SAMPLE CODE..
CREATE VIEW V_Stuff WITH SCHEMABINDING AS
SELECT
MD.ID,
MD.[DateTime]
...
dbo.FN_DayNumeric_DateTime(MD.DateTime) AS [Day],
dbo.FN_TimeNumeric_DateTime(MD.DateTime) AS [Time],
...
FROM {SOMEWHERE}
GO
CREATE UNIQUE CLUSTERED INDEX V_Stuff_Index ON V_Stuff (ID, [DateTime])
GO
Ok, i figured it..
CREATE FUNCTION [dbo].[FN_DayNumeric_DateTime]
(#DT DateTime)
RETURNS INT WITH SCHEMABINDING
AS
BEGIN
DECLARE #Result int
DECLARE #FIRST_DATE DATETIME
SELECT #FIRST_DATE = convert(DATETIME,-53690+((7+5)%7),112)
SET #Result = datediff(dd,dateadd(dd,(datediff(dd,#FIRST_DATE,#DT)/7)*7,#FIRST_DATE), #DT)
RETURN (#Result)
END
GO
Slightly similar approach to aforementioned solution, but just a one-liner that could be used inside a function or inline for computed column.
Assumptions:
You don't have dates before
1899-12-31 (which is a Sunday)
You want to imitate ##datefirst = 7
#dt is smalldatetime, datetime,
date, or datetime2 data type
If you'd rather it be different, change the date '18991231' to a date with the weekday that you'd like to equal 1. The convert() function is key to making the whole thing work - cast does NOT do the trick:
((datediff(day, convert(datetime,
'18991231', 112), #dt) % 7)
+ 1)
I know this post is way-super-old, but I was trying to do a similar thing and came up with a different solution and figured I'd post for posterity. Plus I did some searching around and did not find much content on this question.
In my case, I was trying to use a computed column PERSISTED, which requires the calculation to be deterministic. The calculation I used is:
datediff(dd,'2010-01-03',[DateColumn]) % 7 + 1
The idea is to figure out a known Sunday that you know will occur before any possible date in your table (in this case, Jan 3 2010), then calculate the modulo 7 + 1 of the number of days since that Sunday.
The problem is that including a literal date in the function call is enough to mark it as non-deterministic. You can work around that by using the integer 0 to represent the epoch, which for SQL Server is Jan 1st, 1900, a Sunday.
datediff(dd,0,[DateColumn]) % 7 + 1
The +1 just makes the result work the same as datepart(dw,[datecolumn]) when datefirst is set to 7 (default for US), which sets Sunday to 1, Monday to 2, etc
I can also use this in conjunction with case [thatComputedColumn] when 1 then 'Sunday' when 2 then 'Monday' ... etc. Wordier, but deterministic, which was a requirement in my environs.
Taken from Deterministic scalar function to get week of year for a date
;
with
Dates(DateValue) as
(
select cast('2000-01-01' as date)
union all
select dateadd(day, 1, DateValue) from Dates where DateValue < '2050-01-01'
)
select
year(DateValue) * 10000 + month(DateValue) * 100 + day(DateValue) as DateKey, DateValue,
datediff(day, dateadd(week, datediff(week, 0, DateValue), 0), DateValue) + 2 as DayOfWeek,
datediff(week, dateadd(month, datediff(month, 0, DateValue), 0), DateValue) + 1 as WeekOfMonth,
datediff(week, dateadd(year, datediff(year, 0, DateValue), 0), DateValue) + 1 as WeekOfYear
from Dates option (maxrecursion 0)
There is an already built-in function in sql to do it:
SELECT DATEPART(weekday, '2009-11-11')
EDIT:
If you really need deterministic UDF:
CREATE FUNCTION DayOfWeek(#myDate DATETIME )
RETURNS int
AS
BEGIN
RETURN DATEPART(weekday, #myDate)
END
GO
SELECT dbo.DayOfWeek('2009-11-11')
EDIT again: this is actually wrong, as DATEPART(weekday) is not deterministic.
UPDATE:
DATEPART(weekday) is non-deterministic because it relies on DATEFIRST (source).
You can change it with SET DATEFIRST but you can't call it inside a stored function.
I think the next step is to make your own implementation, using your preferred DATEFIRST inside it (and not considering it at all, using for example Monday as first day).
The proposed solution has one problem - it returns 0 for Saturdays. Assuming that we're looking for something compatible with DATEPART(WEEKDAY) this is an issue.
Nothing a simple CASE statement won't fix, though.
Make a function, and have #dbdate varchar(8) as your input variable.
Have it return the following:
RETURN (DATEDIFF(dd, -1, convert(datetime, #dbdate, 112)) % 7)+1;
The value 112 is the sql style YYYYMMDD.
This is deterministic because the datediff does not receive a string input, if it were to receive a string it would no longer work because it internally converts it to a datetime object. Which is not deterministic.
Not sure what you are looking for, but if this is part of a website, try this php function from http://php.net/manual/en/function.date.php
function weekday($fyear, $fmonth, $fday) //0 is monday
{
return (((mktime ( 0, 0, 0, $fmonth, $fday, $fyear) - mktime ( 0, 0, 0, 7, 17, 2006))/(60*60*24))+700000) % 7;
}
The day of the week? Why don't you just use DATEPART?
DATEPART(weekday, YEAR_DATE)
Can't you just select it with something like:
SELECT DATENAME(dw, GETDATE());

Resources