I have a stored procedure which deletes the records from DB based on following logic.
Select Id into #deleterecords from pricetable where
((Convert(date, modifiedon) <> convert(date,gatedate())
I would like to add one more day in Gatedate()
I’m trying in this
Select Id into #deleterecords from pricetable where
((Convert(date, modifiedon) < > convert(date,gatedate()+1)
It’s not working.
How can I add one more day?
What do you mean by "It’s not working."? Are you getting some error message?
Did you mean to write GETDATE() (the built-in function that returns the current timestamp), or you indeed have a gatedate() function in your database?
To add a day to a datetime value in SQL Server use the function DATEADD.
select Id
into #deleterecords
from pricetable
where
convert(date, modifiedon) <> convert(date, DATEADD(day, 1, gatedate()))
;
I am trying to create a report that will forecast future gains. I am trying to find a way for my column names to update themselves over time from current month. My approach was to create the same case statement bellow for my future months by inclemently adding 1 to the month. However, it seems that I cannot use functions as ALIAS. What would you recommend I do?
CASE
WHEN DATEDIFF(month, GETDATE(), j.Estimated_Start_Date) <= 0 AND
DATEDIFF(month, GETDATE(),j.Estimated_Comp_Date) >= 0
THEN 1
ELSE 0
END AS **CONCAT(DATENAME(MONTH,j.Estimated_Start_Date),'-', DATENAME(YEAR,j.Estimated_Start_Date))**
I've tried searching this up, but nothing really matched with what I was looking for, so any help is appreciated!
And what I want is to have an Expression or flag which should return true if the number of days between previous closed date and current Start date is less than 10 for the TASK ID.
For Example The above table should looks like
Note:
I only have read-only access, so 'update queries' and 'create new table' queries are not applicable for my case.
In SQL Server, you can use lag():
select t.*,
(case when lag(close_date) over (partition by task_id) < dateadd(day, -10, close_date)
then 1 else 0
end) as flag
from t;
I'm retrieving data from a view, and one of the columns I'm using is nvarchar(50), but is only ever N'True' or N'False', depending on the operation of a related date column in this parent view.
The following code retrieves the record ID and the column I'm looking for, YTD:
SELECT Enquiry_Number, YTD
FROM dbo.vw_SalesPO_YTD
Output:
ENQ-001 True
ENQ-002 False
ENQ-003 True
However, I'm unable to filter my results using this YTD column for some reason. If I attempt to do this:
SELECT Enquiry_Number, YTD
FROM dbo.vw_SalesPO_YTD
WHERE YTD = N'True'
Then it fails with the following error:
Arithmetic overflow error converting expression to data type datetime.
Which I don't understand because there are no datetime expressions in play in this query. Yes the True or False was determined by comparing datetimes in the parent view, but I don't understand how that might have trickled down to this subquery. Attempting the same thing in the parent view yields the same error - I'm demonstrating it this way for simplicity's sake.
However, performing a similar operation in the SELECT portion of the query works without issues:
SELECT
Enquiry_Number,
YTD,
CASE
WHEN YTD = N'True' THEN 1 ELSE 0
END As C
FROM
dbo.vw_SalesPO_YTD
Output:
ENQ-001 True 1
ENQ-002 False 0
ENQ-003 True 1
However, these 1's and 0's inherit the same flaw, where I can't use them in a WHERE clause without getting this datetime error.
I've been searching hard and am not sure how to identify the core issue. I've been reading things about Collations and type precedence, but can't understand why this behaviour is happening.
When I've checked YTD in INFORMATION_SCHEMA.COLUMNS, it confirms that this column is no different from other columns in my table: YTD is nvarchar(50), using the Latin1_General_CI_AS collation.
Related question: SQL Server Arithmetic overflow error converting expression to data type datetime
The Source of the Problem
This issue is still unsolved, but if you wish to reproduce it, this code from the parent view must be generating this issue:
CASE WHEN
Award_Date <= DATEFROMPARTS(FinancialYear - 1, 11, 1) + GETDATE() - DATEADD(year, DATEDIFF(month, '20161101', GETDATE()) / 12, '20161101')
THEN N'True'
ELSE N'False'
END
Yes this looks overly complicated. We're checking the Award_Date against its associated FinancialYear, which runs from November 1st to October 31st. Each record already knows which FinancialYear it's in. The ultimate aim is to compare TODAY's position (2016-11-30) against TODAY last year (2015-11-30), and TODAY the year before (2014-11-30), etc.
So the code takes today's date and combines it with the FinancialYear for the associated record, and spits out whether the record had occurred between the start of its financial year and the today of the same year. And it's doing this successfully, but then I can't do anything with the N'True' or N'False' it's producing.
I do not know what the type of the source YTD is.
Try using the following:
SELECT Enquiry_Number, [YTD] FROM (
SELECT Enquiry_Number, CONVERT(nvarchar(10),YTD) AS [YTD] FROM dbo.vw_SalesPO_YTD
) AS A
WHERE A.YTD = N'True'
10 is just a thump suck value. It will cut of any part of the field longer that 10. It depends on your actual field size.
SELECT *
FROM (SELECT Enquiry_Number, YTD
FROM dbo.vw_SalesPO_YTD) AS A
WHERE cast(A.YTD as varchar) = 'True'
I used the following as an example:
DECLARE #Data TABLE
(
Enquiry_Number nvarchar(10),
YTD nvarchar(50)
)
INSERT INTO #Data(Enquiry_Number, YTD)
SELECT N'ENQ-001', N'True' UNION
SELECT N'ENQ-002', N'False' UNION
SELECT N'ENQ-003', N'True'
SELECT Enquiry_Number, [YTD] FROM (
SELECT Enquiry_Number, CONVERT(nvarchar(10),YTD) AS [YTD] FROM #Data
) AS A
WHERE A.YTD = N'True'
Result:
ENQ-001 True
ENQ-003 True
There must be results in the YTD field that causes to return it as a datetime type.
Try a query like:
SELECT * FROM dbo.vw_SalesPO_YTD WHERE ISDATE(YTD)= 1
Updated Question:
Try:
ISNULL(CASE WHEN Award_Date <= DATEFROMPARTS(FinancialYear - 1, 11, 1) + GETDATE() - DATEADD(year, DATEDIFF(month, '20161101', GETDATE()) / 12, '20161101') THEN N'True' ELSE N'False' END, 'false')
Try this:- Don't add n before string
SELECT *
FROM (SELECT Enquiry_Number, YTD
FROM dbo.vw_SalesPO_YTD) AS A
WHERE A.YTD = 'True'
This is a successful workaround, not a solution per se or explanation of the core issue.
There's obviously an issue with the data coming out of the view, which will not allow YTD column's results to be operated on in a WHERE clause, and yet they can be operated on by the time the query reaches its SELECT phase.
I've created a new table which explicitly defines the YTD column as nvarchar(50), and then inserted all the records from my view into this table, which has resolved the issue. The records can then be sorted and filtered by YTD as they are supposed to.
I'm struggling with my school assignment, where I have to update a table column based on the difference between two dates. If the difference is lower or equal than column 'maturity' in the table, I had to set column 'Payment' to 'PaymentOK', if the difference is greater than 'maturity', the 'Payment' has to be set to 'Bilker'.
I've tried
#IFNOTFieldExists(TableName=Invoices;FieldName=Payment;)
Alter table Invoices ADD COLUMN Payment TEXT
GO
UPDATE Invoices SET Payment = CASE
WHEN DATEDIFF (Day, Datum, ZaplateneDna) <= Maturity
THEN 'PaymentOK'
ELSE 'Bilker'
END
#ENDIF
Been trying IF THEN statements, UPDATE WHEN CASE statements but I'm still getting syntax errors.
I'm a bit new in sql and I'm unable to combine UPDATE column1 IF DATEDIFF is <= or > then SET Column2 to (based on the <> sign).
Any ideas or advices please?
Thank you in advance, much appreciated.
UPDATE Invoices
SET Payment = CASE
WHEN DATEDIFF(DAY, [Date], DateOfPayment) <= Maturity
THEN 'PaymentOK'
ELSE 'Bilker'
END