Getting the last day of an already existing date in SQL Server Management Studio [duplicate] - sql

This question already has answers here:
SQL Query to find the last day of the month
(15 answers)
Closed 5 years ago.
I have a table with a list of clients, with a column that shows the date their accounts were created. I want to make the last day of this date. I tried using the EOMONTH function, but it does not work.
For instance, if client 1 came on January 6th, and client 2 came on February 6th of this year, I want it to show 31-01-2018 and 28-02-2018.
Any ideas?
Thanks.

Gordon is right, I have tried EOMONTH for your given data and it returns perfect value look at below, in second parameter you have to pass value as per requirement if you pass value as 1 it will give you last date of next month i.e Jan+1=Feb
DECLARE #myDate Datetime='06-Jan-2018'
SELECT EOMONTH(#myDate,0) AS MyDate
If your version not support the EOMONTH function then try below query:
SELECT CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#myDate)+1,0)) AS DATE)
Output:

Related

How do I find the first and last day of the previous month using SQL?

I am using IBM Data Studio to access data on an AS400, I cannot figure out how to programmatically find the first and last date of the previous month to use in an BETWEEN clause. For example, if I ran it today it would return 20211201 and 20211231. I have done some other date math in the past but this one is beyond me. Below is an example of a date function I have used successfully in this version of SQL.
{DEC(DATE(DAYS((CURRENT_DATE) - 15 DAY)))}
Try
DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 MONTH
and
DATE(YEAR(CURRENT_DATE)||'-'||MONTH(CURRENT_DATE)||'-1') - 1 DAY

SQL Server - Select all records whose dates are within 5 days of target date [duplicate]

This question already has an answer here:
SQL Server Management Studio make default date 5 days from now
(1 answer)
Closed 4 years ago.
I need to create a view everyday with all records reaching the "Prazo" date ('Prazo' stands for due date in Portuguese), so everyday I need to make a select showing all the records that are 5 days from reaching the due date. How can I do that?
You can do:
Select *
from Table as t
Where DATEDIFF(DD, GETDATE(), PrazoDate) = 5
It just says how many records from today are 5 days from reaching their due date.

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

How do you calulate last day of the month from datetime value [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I calculate the last day of the month in SQL?
New to SQL, but how would you determine the last day of the month value from datetime (trans_date).
Sorry just a dumb question, but rather than using current date can I specify a trans_date (date) from trans table
Easy, find the first of the following month and subtract 1 day.
eg,
DECLARE #dt AS DATETIME = GetDate();
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dt)+1,0))
EDIT: If you're selecting from a table just do this:
SELECT
SomeColumn,
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,SomeColumn)+1,0)) AS EndOfMonth
FROM SomeTable
Pinal Dave has a good explanation here:
http://blog.sqlauthority.com/2007/08/18/sql-server-find-last-day-of-any-month-current-previous-next/
Keep in mind that if you're using SQL2008 you can CAST( field AS DATE) to get rid of the resulting time data.

SQL date ranges [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
90 days range using SQL server
I am trying to get the counts 90 days prior to the operational date and the counts 90 days after the operational date. For example, my operational date is 4/1/2004. So,90 days prior to 4/1/2004 is (1/2/2004 to 3/31/2004) and 90 days after (including 4/1/2004) is 6/29/2004.
I used the following scripts and mannually calculate the days, which is not efficient...
select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site
select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
You should look at the DATEDIFF function or equivalent if you're not using SQL Server.
DATEDIFF on MSDN
If you are passing the date parameter in from an application, consider modifying the application to do the date range calculation for you. By passing in two parameters, you are taking the burden of the calculation off SQL, which will improve performance.