Month and Year Part from GetDate - sql-server

I am using a gridview in my asp.net application in which i want to limit the dataset to requests that are submitted in the current month and year
Iam new to application development and thinking in the below way
to get the month and year part from the getdate() function and then compare it with the month and year of the receiveddate column in our table in the where condition
My select statement would be like below
select requestno,receiveddate,businessneed from customerrequests
where receiveddate = getdate()
in the above sql statement how do i acheive

There are two ways you could do this:
In SQL by passing two parameters for month and year. This is what your questions sounds like and can be done by just doing this:
select requestno,receiveddate,businessneed from customerrequests where YEAR(receiveddate) = YEAR(getdate()) AND MONTH(receiveddate) = MONTH(getdate())
The other way would be to construct a date object on the client side and pass that in to your query and then just use regular date comparison techniques.
Hope that helps.

You can do something like below:
DECLARE #FirstDate DATETIME
SELECT #FirstDate = DATEADD(month, DateDiff(Month, 0, GETDATE()), 0)
SELECT
requestno,receiveddate,businessneed
FROM
customerrequests
WHERE
receiveddate >= #FirstDate
Hope this Helps!!

Related

How to get newly inserted records in SQL Server per day

I am using a SQL Server database, and I need to get the total number of newly inserted records per day, per week, per month, and per year separately. I have a column in my SQL Server database that records the date and time (the data type is datetime).
I used the following code to get the daily records but doesn't work
SQL:
select count(*)
from dbo.Firsttimer
where (Signed_in) = date(date_sub(now());
Please how do I achieve this?
You'll need to use conditional aggregation, i.e. a count(case...), along with the getdate() and dateadd() functions.
select
Today = cast(getdate() as date)
,TodayCount = count(case when cast(Signed_in as date) = cast(getdate() as date) then Signed_in)
,RollingWeekCount = count(case when cast(Signed_in as date) >= dateadd(day,-7,cast(getdate() as date)) then Signed_in)
,Rolling30Days = count(case when cast(Signed_in as date) >= dateadd(day,-30,cast(getdate() as date)) then Signed_in)
,Rolling365Days = count(case when cast(Signed_in as date) >= dateadd(day,-365,cast(getdate() as date)) then Signed_in)
from
YourTable
If you wanted this split out for each week, each month, each year... then that's best handled another way. However you didn't specify this so I provided a rolling method.
In SQL Server, you get the current DateTime value using the built in GetDate() method.
So to get all the records that have today's date in a specific DateTime column you can do something like this:
DECLARE #FromDate date = CAST(GETDATE() AS date)
DECLARE #ToDate date = DATEADD(DAY, 1, #FromDate)
SELECT COUNT(*)
FROM dbo.Firsttimer
WHERE [Signed_in] >= #FromDate
AND [Signed_in] < #ToDate
To get the first and last day of the week you can read this SO post,
first day and last day of the month on this SO post,
and first and last day of the year on this SO post

SQL: Query where date is like today

I have a little problem with my SQL Query:
I have this value on my table: 2014-10-23 00:00:00
I have a record like this each 2 minutes every day, and I need to SELECT all the value of today.
Now I made this:
WHERE mytable.data LIKE CURDATE()
and it doesn't work. I tried a lot of things found here on stackoverflow and nothing could help me.
Thanks for answer.
You don't say which version of SQL Server you are using, if you have the DATE data type available you can cast the datetime returned by getdate() or CURRENT_TIMESTAMP to strip off the time part.
But then you need a range to find all the matching rows, something like:
SELECT *
FROM mytable
WHERE mytable.data >= CAST(GETDATE() AS date)
AND mytable.data < CAST(DATEADD(day, 1, GETDATE()) AS date)
SQL Fiddle

differences between dates in sql

I have two columns namely enddate and startdate
I need to find all those records where
(enddate-startdate)>2 years
I tried using timediff(enddate,startdate)>2
But it returns an error saying : 'timediff' is not a recognized built-in function name
I am using sql server management studio, any help ?
My date format is 2007-04-16 00:00:00.000
Try to use DateDiff
DateDiff(year,enddate,startdate)
WHERE dateadd(year, -2, enddate) > startdate
I would use DATEDIFF. It should get you there at a macro level.
DECLARE #orderTracker TABLE (
orderDate DATE,
orderShipped DATE,
orderNum VARCHAR(6)
);
INSERT INTO #orderTracker
( orderDate, orderShipped, orderNum )
VALUES ('01/01/2012', '06/12/2013', '55YY7'),
('05/20/2006', '09/10/2008', 'sdlhf8'),
('06/02/2011', '10/12/2012', '34JJU'),
('11/25/2009', '06/12/2013', '553iSS'),
('06/15/2008', '12/12/2011', '5F09U7'),
('02/06/2013', '08/12/2013', '55YY7');
SELECT * FROM #orderTracker;
SELECT *
FROM #orderTracker
WHERE DATEDIFF(YEAR, orderDate, orderShipped) >= 2;
Use DateDiff
WHERE DATEDIFF(year, startdate, enddate) > 2
EDIT: Duplicated, I had not seen the answer of Jayesh Goyani, sorry!
From discussion in comments, your best bet is to establish a threshold date using the start datetime and dateAdd function. This will give you all records where the end time is after the datetime at the same time of day, on the same calendar date (month, day of month), two years after the start date.
Where endDate > dateadd(year, 2, startDate)
Please use 'Year' in DateDiff
DATEDIFF(year, startdate, enddate) > 2
For more information please check below link.
http://msdn.microsoft.com/en-us/library/ms189794.aspx
This should return the number of years:
DATEDIFF(year,'2008-06-05','2008-08-05')
DATEDIFF does not work correctly for this situation as of the time of me writing this. Actually, it doesn't work correctly at all and no one should use it.
Example being;
DATEDIFF(YEAR, '2016-08-28', '2017-04-28')
Returns 1, although it has not been a year. It simply subtracts the years and doesn't take into account the months or days.
Use the DATEADD method instead, as others have suggested.

How to compare two date and to find out whether the date difference is within 3 months in SQL Server?

Does anyone know how to compare two date and to find out whether the date difference is within 3 months in SQL Server?
Example if I have 2 dates:
Start_Date = '2013-02-01'
End_Date = '2013-04-10'
How can I compare two date and to find out whether the date difference is within 3 months in SQL Server?
Look into using DATEDIFF:
SELECT *
FROM YourTable
WHERE DATEDIFF(month, start_date, end_date) <= 3
SQL Fiddle Demo
MSDN: http://msdn.microsoft.com/en-us/library/ms189794.aspx
Here's the answer using the two dates you provided in your question. I gave it to you two ways. The first column is the comparison of whether it is within 3 months. The second column is simply the result of the SQL Server system function DATEDIFF.
DECLARE #Start_Date DATE = '2013-02-01',
#End_Date DATE = '2013-04-10'
SELECT CASE
WHEN datediff(MONTH, #Start_Date, #End_Date) <= 3 THEN
'True'
ELSE
'False'
END AS [WithinThreeMonths]
, datediff(MONTH, #Start_Date, #End_Date) [Months]

SQL Query-calculating last week data for an ordinary calendar year

I have to retrieve the last week data from a table.
i am using the following condition.
#prmCurrent_Year=Datepart(year,getdate())
last_week=case when Datepart(week,col_name)=1
then 52
else Datepart(week,col_name)-1 and
year_num=case
when Datepart(week,col_name)=1
then #prmCurrent_Year-1
else #prmCurrent_ Year
will this work properly or is there any other better query for this???
Something like this might suffice:
SELECT *
FROM Table
WHERE MyDate BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()
DateAdd reference on MSDN: http://msdn.microsoft.com/en-us/library/ms186819.aspx

Resources