This is my request: based on Review Date, populate the month of review in MM/DD/YYYY format. E.g. if Review Date is 8/17/2016, Month of Review should populate 8/1/2016.
Originally, I had it coded
datename(month, c.CreatedDate) as 'Month'
but requestor now wants it 8/1/2016 instead of 'August' regardless the day of the month.
Thank you
You can use dateadd and eomonth functions
dateadd(day, 1, eomonth(dateadd(month, -1, your_date)))
EDITED
Sorry, eomonth is available since SQL Server 2012
select dateadd(day, -(datepart(day, your_date))+1, CONVERT(DATE, your_date, 101))
Related
I want to make my query more flexible with dates. I am thinking of defining variables with reference to current date. currently I am coding the exact date for the last quarter, the quarter before and the quarter one year ago.
That is the query for Q1:
select '2020_Q1' as time_frame, id, status, date, agent, country, sale
from sales
where date >= '2020-01-01' and date < '2020-03-31'
I do the same for Q4 and Q1 (2019) and union in the end. Today is April 27, let's say it is 4 months from now and it is August 27th. Now, I would want to look at Q2,Q1 and Q2(2019). I believe, I would need to work with current_date, but let me know if you think there is a more efficient way.
You can use date_trunc() to get the start of the "current quarter":
select ...
from sales
where date => date_trunc('quarter', current_date)
and date < date_trunc('quarter', current_date) + interval '3 months'
What is the most efficent way in T-SQL (2005) to get results from X date til today?
Attribute is RegistrationDate and is in date/time format, such as Monday, July 15, 2019 12:00 AM
I'm looking to get records where the RegistrationDate is from 20th August til Today, as this query would be running daily.
Current query is having some issues with this requirement:
cast(a.RegistrationDate as date) = cast(GETDATE() BETWEEN '2019-20-10 00:00:00.0' and GETDATE() as date)
SQL Server 2005 is long out of support, why are you still using it? If you really are still using SQL Server 2005 it's long past time you upgrade and you really need to look at upgrade paths asap. No supported versions of SQL Server support an upgrade from 2005; the newest version is 2014 which is in Extended support only.
For what you're asking, however, apart from the obvious syntax errors, the date datatype does not exist in such as old version of SQL Server. The best method is therefore use use DATEADD and DATEDIFF. I also assume that your statement " is in date/time format, such as Monday, July 15, 2019 12:00 AM" is misinformed; datetime datatypes don't have a format.
With all that I would (if I had to) do the following:
DECLARE #DateStart = '20190101',--First date in range
#DateEnd = '20200101'; --First date outside of range
SELECT ...
FROM ...
WHERE DateTimeColumn >= #DateStart
AND DateTimeColumn < #DateEnd;
If you want to make GETDATE() the start of the (current) day, then you would do:
DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
I have a list of employees and their hire dates. I need to pull revenue records for their first 3 months on the job. For example, if someone is hired on August 6 2018, I need to pull their revenue records for September, October, and November 2018.
I really just need the "where" statement to find the correct dates.
Let's call the tables: bookings, employees
columns: hired_at, booked_at
I originally thought it was first 3 months from hire date, and I used
where bookings.booked_at <= dateadd(month, 3, employees.hire_date)
Upon finding out that I need the 3 months FOLLOWING the hire date, I'm not sure how to write this.
Thanks!
You should use INTERVAL, dateadd isn't supported by PostgreSQL
<= employees.hire_date - Interval '3 months'
You can check the documentation in the following link
https://www.postgresql.org/docs/9.0/functions-datetime.html
WHERE booked_at >= date_trunc('month', hired_at + interval '1 month')
AND booked_at < date_trunc('month', hired_at + interval '3 month')
where date_trunc('month', ...) always gives you the 1st of the month.
Sorry if this has been asked somewhere all ready but I've struggled to find the answer! I have a date time field with is a date time stamp on activity.
I need to have a query that only brings back information for data that's between today's date and events between 08:00 and 10:00.My brains says it should be straight forward but i cant suss it.
Thanks
To get a particular point in time, I tend to use the DATEADD/DATEDIFF pattern. It can look a bit unwieldy, but performs quite nicely, and once you're used to seeing it, it becomes more readable:
SELECT * --TODO - Pick columns
FROM Activities
WHERE OccurredAt BETWEEN
DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00') AND
DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T10:00:00')
Here I'm using it twice. It just relies on the relationship between two date constants that you pick because they exhibit the features that you want. So here:
DATEADD(day,DATEDIFF(day,'20010101',GETDATE()),'2001-01-01T08:00:00')
I'm calculating the number of (whole) days that have elapsed between 1st January 2001 and right now. I'm then adding that (whole) number of days onto 08:00 on the 1st January 2001 - which will, logically, produce 08:00 today.
Here you go...
SELECT * FROM <TABLE_NAME> WHERE EVENTS_DATE BETWEEN
DATEADD(hour, 8, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
AND
DATEADD(hour, 10, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
If you use a variable in TSQL then store today's date in that
DATEADD(day, DATEDIFF(day, 0, GETDATE())
select ...
where dateColumn between
convert(varchar(10), getdate(), 120) + 'T08:00:00'
and convert(varchar(10), getdate(), 120) + 'T10:00:00'
This should not be this hard. I simply need the following:
SET #DueDate = CONVERT (DATETIME, '01/01/2010')
However, I need it pragmatically so that if it were March of 2010, the date given would be '01/01/2011'.
I know it's simple, but my brain isn't coming up with it. I'm sure it's with a DateAdd and getdate().
Number of year boundaries between now and year zero less one (31 dec 1899), add back on.
SELECT DATEADD(year, DATEDIFF(year, -1, GETDATE()), 0)
Let's try a date next year to get 2011. because 1 Jan 2010 is start of next year in 2009...
SELECT DATEADD(year, DATEDIFF(year, -1, '2010-03-21'), 0)
Based on the Database Journal article: How to Calculate Different SQL Server Dates:
First Day of the Next Year
I use the year interval (yy) to display the first day of the next year.
select DATEADD(yy, DATEDIFF(yy, -1, getdate()), 0)
Incidentally, with SQL 2012 you can do DATEFROMPARTS(YEAR(#date) + 1, 1, 1). In your case, DATEFROMPARTS(YEAR(GETDATE()) + 1, 1, 1)
Hat tip to https://stackoverflow.com/a/13873750/155892
Perhaps, create a table with first of the year dates, then pick the smallest that is larger than your date.
Perhaps simply put a string together, '01/01' + (MyYear + 1)?
You could add one to the current year, then concat with Jan 01, then convert back to date:
select CONVERT(datetime, CONVERT(VARCHAR(20), YEAR(GETDATE())+1) + '.01.01')
Using some parts of the examples above you could also do this. Now this assumes your acceptable date format matches M-d-yyyy.
DATEADD(year, 1,'1-1-'+convert(varchar,DATEPART(YYYY,GetDate())))
Note: You get the current date, get the year part, create a string that converts to a date, then add one to the year.