Enter Parameter Value when getting the last dat of the month - sql

I want to get the last day of the month by using the following formula I found here at Stackoverflow:
SELECT DATEADD(month, ((YEAR(#test) - 1900) * 12) + MONTH(#test), -1)
I'm sure this formula should work but when I use it I get a pop-up where I'm asked to enter a parameter value for Month.
Does anyone know how I could solve this issue?
Thank you in advance.

This looks like SQL Server. If so, just use EOMONTH():
select EOMONTH(#test)
I realize that you might be using MS Access. If that is the case you can use date functions to get the value:
select DateAdd(d, -1,
DateAdd(m, 1,
DateSerial(Year(#test), Month(#test), 1))
)
)

Related

How to use SQL - DATEDIFF function

need quick help here.
I'm coming up with a column to track how many days a event is back-logged.
So I'm using this syntax which is working
DATEDIFF(DAY, GETDATE(), ESI.EventStatusDate) AS BackloggedDays
However, the modification I want to use is, the event status date + 3 days. Any ideas how i would add that to this syntax. Thanks
I think you need DATEADD() :
DATEDIFF(DAY, GETDATE(), DATEADD(DAY, 3, ESI.EventStatusDate)) AS BackloggedDays

DateAdd function in Excel SQL query not working

I am using Excel to make an SQL query (connecting to an ODBC).
I have a problem with the function DateAdd(), which just doesn't work as expected.
What I need to do is to get the data from the last six months.
My code is something like
SELECT blablabla
FROM blablabla and then I have this:
WHERE Note_0.Relate_key = Work_history_0.WO_Key AND Work_history_0.Order_date> DateAdd(Month, -6, Now())
I've searched in the internet and this syntax is supposed to work, but I get this error message
Column "MONTH" cannot be found or is not specified for query. (13865)
As if it didn't have the parameters I think it has, which are "interval, number, date", but something else.
Any idea about this?
This is what you need:
DateAdd("m", -6, Now)
Or even DateAdd("m", -6, Date), if you want to get rid of the hours, coming from Now.
Thus, you have to declare that you want the "Months" to be substracted.
DateAdd MSDN
WHERE Note_0.Relate_key = Work_history_0.WO_Key AND Work_history_0.Order_date > ADD_MONTHS(curdate(), -6)

Using DateDiff() to find the difference between getDate() and a concatonated value

I am trying to find the difference between today's date and a value that is a concatenation of mulitple values but begins with an 8 digit date without any dashes or forward slashes. There's something wrong with my syntax I believe, but I'm not yet skilled enough to see what I'm doing incorrectly. Here is what I have so far:
select DateDiff(dd, (select MIN(CAST(Left(batchid, 8) as Date)) from
[Table]), getdate()) from [Table]
This is returning the following error: "Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string."
I think your have data where the left 8 is not a valid date in yyyymmdd format. Your can run the following query to find them
select batchid, isdate(Left(batchid, 8))
from [Table]
where isdate(Left(date, 8)) = 0
This is the correct syntax to your query. Your original example had an extra parenthesis which I assume was a typo since your error appears to be data related.
select
datediff(dd, (select min(cast(left(batchid, 8) as date))
from [Table]), getdate())
Could you provide some more details.
Namely, what does batchid look like in 8 digit form? is it YYYYMMDD or DDMMYYYY or MMDDYYYY?
Also could you show us the result of the following?
select MIN(CAST(Left(batchid, 8) as Date)))
from [Table])
Sry for using an answer, i don't have the rep to add a comment directly below.
This was may error. I was working with another table and forgot batchID was not the same for both. The concatenated batchID in the table I posted a question about can't be converted to a date.

SQL get last month name

Following command giving me current month (March) in short form.
SELECT left(datename(month, getdate()), 3)
But i need last month Feb.
I meant whenever i run this command will give me last month name.
What would be the sql command?
Assuming you are using T-SQL:
SELECT left(datename(month, dateadd(month,-1,getdate())), 3)
http://msdn.microsoft.com/fr-fr/library/ms186819.aspx
SELECT left(datename(month, dateadd(dd, -1, getdate())), 3)
try this:
SELECT left(datename(month, dateadd(m,-1,getdate())), 3)
Try this:
SELECT left(datename(month, date_sub( getdate(), interval 1 month ) ), 3)
This will work for MySQL (you didn't specify your SQL server), might need something similar to date_sub for different DBs.
SELECT left(datename(month, DATEADD(MM,-1,getdate())), 3)
select left(datename(month, dateadd(month, -1, getdate()),3)
SELECT DATENAME(MM,DATEADD(MM,-1,GETDATE())) AS 'The Name of last Month'
to print Name of the particular day we have to use DATENAME() function. So, we used this function here for that purpose.
MM represents month here.
-1 is used in Date function to get previous month.
Assuming TSQL, you're going to want to incorporate the DATEADD() function into that query:
http://msdn.microsoft.com/en-us/library/ms186819.aspx
wrap getdate in that and you should be golden.
Try this:
select substring(DATENAME(MONTH,getdate()),1,3)
i think it will help you much better
Regards,
Azarudhin S.

sql compare datetime today

I hope anyone can translate my abstract query.
I want to select * from TABLE where ( [MYDATETIMEROW] < (TODAY - 3 Days)).
Does I have to Convert, cast or use datepart or anything else?.. im confused.
Are there simple rules? I would'nt have problems to do that with linq but simple sql I learned just hardly.
Thank you and best regards.
In simple terms:
Select * from Table where MyDateTimeRow < dateadd(dd,-3,getdate())
But using getdate() will provide both a date and a time, experience says that this is unlikely to be exactly what you want - you might want to strip the time down and just consider the date portion
Select * From Table where MyDateTimeRow < dateadd(dd, datediff(dd, 0, getdate()) - 3, 0)
You want the DateAdd function to manipulate dates and the GetDate function to get the current date:
SELECT * FROM MyTable WHERE [MyDateTimeRow] < DateAdd(dd, -3, GetDate())