Sum of Dates between, T-SQL error - sql

I'm trying to check if one of the join_date or date_of_change (date fields) are within the range and count them but I get an error:
sqlserver.jdbc.SQLServerException: The conversion from int to
TIMESTAMP is unsupported.
SUM(CASE WHEN (join_date BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) OR (date_of_change BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) THEN 1 ELSE 0 END) AS Total
Could someone explain to me what I'm doing wrong.
Original Code:
SELECT DISTINCT mtype, CASE WHEN (join_date BETWEEN DATEADD(day, -8,
GETDATE()) AND DATEADD(day, -1, GETDATE())) OR (date_of_change BETWEEN
DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) THEN 1 ELSE 0
END AS Total FROM T0 GROUP BY mype, join_date,date_of_change

As #Alex K has said there could be another statement in the batch causing the problem as there doesn't seem to be any TimeStamp involved in this query.
Answering your last comment about the GROUP BY, you can simplify the query the following way:
SELECT
mtype, COUNT(1) as Total
FROM
T0
WHERE
(join_date BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE()))
OR (date_of_change BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE()))
GROUP BY
mype
But I am afraid that if the error is in a different statement.

I wrote the query again and it worked, must be a typo somewhere

Related

SQL: Pull previous day's activity (include weekend on Mondays)

I'm trying to return data from the previous day, except for on Monday I want to return data from the previous 3 days. The below seems logical to me, though I'm getting the error of Incorrect syntax near '='.
Any idea what I'm missing?
*Dates stored in datetime hence the CONVERT functions.
WHERE
CASE WHEN DATEPART(DW,GETDATE()) IN ('1')
THEN (CONVERT(DATE,EV.EVENT_DATE) = CONVERT(DATE,DATEADD(D,-3,GETDATE())))
ELSE (CONVERT(DATE,EV.EVENT_DATE) = CONVERT(DATE,DATEADD(D,-1,GETDATE())))
END
SQL Server does not support boolean expressions like this.
You can express this without the case, which is generally preferable:
WHERE ( DATEPART(WEEKDAY, GETDATE()) = 1 AND CONVERT(DATE, EV.EVENT_DATE) = CONVERT(DATE, DATEADD(DAY, -3, GETDATE()))
) OR
( DATEPART(WEEKDAY, GETDATE()) <> 1 AND CONVERT(DATE, EV.EVENT_DATE) = CONVERT(DATE, DATEADD(DAY, -1, GETDATE()))
)
EDIT:
If you want the weekend dates, then use inequalities. Assuming event_dates are not in the future:
You'll notice that I spelled out the date parts. I find this a better practice than trying to remember/figure out what a particular abbreviation might mean.
WHERE ( DATEPART(WEEKDAY, GETDATE()) = 1 AND CONVERT(DATE, EV.EVENT_DATE) >= CONVERT(DATE, DATEADD(DAY, -3, GETDATE()))
) OR
( DATEPART(WEEKDAY, GETDATE()) <> 1 AND CONVERT(DATE, EV.EVENT_DATE) >= CONVERT(DATE, DATEADD(DAY, -1, GETDATE()))
)

Get data from last month in SQL Server

I checked Get the records of last month in SQL server and it did not work!
I try to get the records of last month based on my database table and column issue_date.
What's the SQL query to do this?
For clarification, today (27-April-18) I want to get all records from March-18.
I have the issue_date in the format that I convert to date but below code gives me all records from 01-March-2018 to and including today.
DATEPART(month, CONVERT (VARCHAR(11),DATEADD(day,wo_header.issue_date,`1971/12/31`),106)) = DATEPART(month, DATEADD(month, -1, getdate()))
To get firstDay and lastDay of previous month
DECLARE #FirstDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))),
#LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE()))
SELECT #FirstDayOfLastMonth, #LastDayOfLastMonth
Your required Query
SELECT *
FROM TABLE
WHERE CAST(issue_date AS DATE) BETWEEN CONVERT(DATE, DATEADD(d, -(DAY(DATEADD(m, -1, GETDATE() - 2))), DATEADD(m, -1, GETDATE() - 1))) AND CONVERT(DATE, DATEADD(d, -(DAY(GETDATE())), GETDATE()))
Perhaps the easiest method is eomonth(). If there is no time component on issuedate:
where issuedate > eomonth(getdate(), -2) and
issuedate <= eomonth(getdate(), -1)
If there is a time component:
where issuedate >= dateadd(day, 1, cast(eomonth(getdate(), -2) as date)) and
issuedate < dateadd(day, 1, cast(eomonth(getdate(), -1) as date))
Without eomonth, I would do:
where issuedate < cast(dateadd(day, 1 - day(getdate()), getdate()) as date) and
issuedate >= dateadd(month, -1, cast(dateadd(day, 1 - day(getdate()), getdate()) as date))
The code I figured out is not pretty but it works. It first adds extra column with the month number to the SELECT portion of my code:
Month(CONVERT (VARCHAR(11),DATEADD(day,wo_header.closing_date,'1971/12/31'),106)) As Month
And than is used for WHERE statement:
Month(CONVERT (VARCHAR(11),DATEADD(day,wo_header.closing_date,'1971/12/31'),106)) = month(getdate())-1
So for anyone like me working in SQL report kind-of environment it should work.

Using CASE in WHERE clause?

I am getting the following error in my WHERE clause on the very last AND statement AND CAST(cp.EndDate...). I just added the CASE statement to know if the month is January and to compare the cp.startdate >= value1 OR value 2
Error:
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
My WHERE clause:
WHERE
(CAST(cp.startdate AS DATE) >= CAST(DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS DATE)
OR --FirstDayOfCurrentMonthPriorYear
CASE
WHEN DATEPART(month,getdate()) = 1 --month is January
THEN CAST(DATEADD(month, DATEDIFF(month, 0, getdate())-1, 0) AS DATE) --FirstDayOfLastMonthPriorYear
ELSE CAST(DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0)) AS DATE) --FirstDayOfLastMonthPriorYear
END)
AND CAST(cp.EndDate AS DATE) <= CAST(DATEADD(day, -1, DATEADD(month, DATEDIFF(month, 0, GETDATE()) + 1, 0)) AS DATE) --LastDayOfCurrentMonthCurrentYear
Not sure what's wrong... any help in implementing this would be appreciated.
From a pure syntax point of view, you need to compare something to your CASE output. Something like this would be syntactically correct.
WHERE
(
CAST(cp.startdate AS DATE) >= CAST(DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) AS DATE)
OR
CAST(cp.startdate AS DATE) >= CASE
WHEN DATEPART(month,getdate()) = 1 --month is January
THEN CAST(DATEADD(month, DATEDIFF(month, 0, getdate())-1, 0) AS DATE)
ELSE CAST(DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, getdate()) -1, 0)) AS DATE)
END
)
AND CAST(cp.EndDate AS DATE) <= CAST(DATEADD(day, -1, DATEADD(month, DATEDIFF(month, 0, GETDATE()) + 1, 0)) AS DATE)
Here is the previous question
I think this is what you are looking for:
WHERE CAST(cp.startdate AS DATE) >= CASE WHEN DATEPART(MONTH,GETDATE()) = 1
THEN DATEADD(DAY,1,EOMONTH(GETDATE(),-1))
ELSE DATEADD(YEAR,-1,DATEADD(DAY,1,EOMONTH(GETDATE(),-1)))
END --FirstDayOfCurrentMonthPriorYear
AND CAST(cp.EndDate AS DATE) <= EOMONTH(GETDATE()) --LastDayOfCurrentMonthCurrentYear
This checks that the cp.startdate is greater than or equal to the first day of the current month in the prior year, and that the cp.enddate is less than or equal to the last day of the current month. Replace GETDATE() with your date of choice.

Get last date of the previous month Transact-SQL

I am trying to fetch data for the previous full month, but the data from the last day is not fetching.
Output: Previous Month is April and this condition only retrieving data up to 29th April. Data for 30th April is missing.
Can please someone help me to correct this.
OLH.DateStamp > CONVERT(VARCHAR,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0),101)
And
OLH.DateStamp < CONVERT(VARCHAR,DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1),101))
Thanks in advance
The simplest way to get data for the previous month is:
DATEDIFF(month, OLH.DateStamp, GETDATE()) = 1
However, that does not use indexes. So, a better method is:
OLH.DateStamp >= DATEADD(MONTH, -1, DATEADD(DAY, 1 - DAY(GETDATE()), CAST(GETDATE() as DATE))) AND
OLH.DateStamp < DATEADD(DAY, 1 - DAY(GETDATE()), CAST(GETDATE() as DATE))
The expression:
DATEADD(DAY, 1 - DAY(GETDATE), CAST(GETDATE() as DATE))
returns midnight on the first day of the current month.
You should try last day of Month Should be <= OLH.Datestamp
To get Desired Output you are Looking for:
--Use It In Where Clause
DECLARE #FirstDayOfLastMonth
DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(DATEADD(m, -1, GETDATE() -
2)) ), DATEADD(m, -1, GETDATE() - 1)))
,#LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE()))
OLH.DateStamp >= CONVERT(VARCHAR, #FirstDayOfLastMonth , 101) AND
OLH.DateStamp <= CONVERT(VARCHAR, #LastDayOfLastMonth , 101)

First business day of the current month - SQL Server

How could I get the first business day of the current month?
Without create a function, only select.
something like that:
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1), GETDATE()), 101)
somebody knows please?
Thanks.
A Simple case statement could do it
SELECT CASE
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Saturday'
THEN dateadd(mm, DATEDIFF(MM, 0, getdate()), 0) + 2
WHEN DATENAME(WEEKDAY, dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)) = 'Sunday'
THEN dateadd(mm, DATEDIFF(MM, 0, getdate()), 0) + 1
ELSE dateadd(mm, DATEDIFF(MM, 0, getdate()), 0)
END
This will literally give you what you're asking for -- the first business day in a month if we define a business day as "any day that's not a Saturday or a Sunday". But this is is a very narrow definition of "business day" that is not appropriate when taking into account holidays and cultural differences, so it generalizes poorly. The typical solution for this problem is to create a table that actually holds the working days (which is generated somewhere just before he year, or calculated in advance if that's feasible), and simply look it up in that.
SELECT DATEADD(DAY,
CASE
(DATEPART(WEEKDAY, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) + ##DATEFIRST - 1) % 7
WHEN 6 THEN 2
WHEN 7 THEN 1
ELSE 0
END,
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
)
This solution uses ##DATEFIRST to avoid any language issues -- using DATEPART(WEEKDAY, ...) on its own or DATENAME() only works if we assume a specific region.
If you have more flexibility in your SELECT statement, you might be able to use something like this:
;With Daterange As
(
Select DateAdd(Month, DateDiff(Month, 0, GetDate()), 0) As Date Union All
Select DateAdd(Day, 1, Date) As Date
From DateRange
Where Date < DateAdd(Day, 6, DateAdd(Month, DateDiff(Month, 0, GetDate()), 0))
)
Select Convert(Date, Min(Date)) FirstBusinessDay
From Daterange
Where DatePart(WeekDay, Date) Not In (7, 1)
Try this.
SELECT CASE
WHEN Datename(dw, Dateadd(dd, -Datepart(dd, Getdate()) + 1, Getdate())) = 'Saturday' THEN Dateadd(dd, -Datepart(dd, Getdate()) + 3, Getdate())
WHEN Datename(dw, Dateadd(dd, -Datepart(dd, Getdate()) + 1, Getdate())) = 'Sunday' THEN Dateadd(dd, -Datepart(dd, Getdate()) + 2, Getdate())
ELSE Dateadd(dd, -Datepart(dd, Getdate()) + 1, Getdate())
END
Explanation :
Find the first day of the month.
Dateadd(dd, -Datepart(dd, Getdate()) + 1, Getdate())
Then check the day of the previous date by using Datename function.
Datename(dw, Dateadd(dd, -Datepart(dd, Getdate()) + 1, Getdate()))
If the Datename is Saturday the add 2 days to the first day of the month . if it is sunday then add 1 day to the first day of the month else get the first day of the month