I want to display Previous month dates.May i know the query which is used to display all dates
Expected Output:
Current date = '2012-09-13'
I want to display my result as
1
2
3
4
,
,
,
,
31
these dates should come from month 8
try this:
SELECT NUMBER
FROM MASTER..SPT_VALUES
WHERE TYPE='P'
AND NUMBER BETWEEN
DATEPART(DD,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1,0))
AND DATEPART(DD,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),-1))
Replcae Getdate() with your date
Please try:
;WITH DATES (date)
AS(
SELECT DATEADD(month, DATEDIFF(month, 0, dateadd(month,-1,getdate())), 0)
UNION ALL
SELECT DATEADD(DAY,1,date)
FROM DATES
WHERE DATEADD(DAY,1,date)<=DATEADD(month, DATEDIFF(month, 0, getdate()), 0)-1
)SELECT DAY(date) AS DAYS FROM DATES
Related
i want to retrieve records where a date field is set to future months
does this look correct
Select * from table1 WHERE
datesetto >MONTH(dateadd(dd, -1, GetDate())))
select * from tablename where month(columndate)>month(getdate()) and
year(columndate)>=year(getdate())
SELECT * FROM table1
WHERE datesetto >= DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0)
Explanation:
DATEDIFF(month, 0, getdate()) calculates how many months have passed since 1900-01-01.
DATEADD(month, DATEDIFF(month, 0, getdate()), 0) returns the beginning of this month.
DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0) returns the beginning of next month.
Try this:
Select * from table1 WHERE
((DATEPART(MONTH,datesetto) > DATEPART(MONTH,GETDATE())
AND DATEPART(YEAR,datesetto) = DATEPART(YEAR,GETDATE()))
OR (DATEPART(YEAR,datesetto) > DATEPART(YEAR,GETDATE())))
DATEPART(Month,GETDATE()) will give the month of the current date and then you can compare it with the datesetto
Update: The above query will give data for any months greater than the current month and any month greater in the year than the current year.
I am trying to get a list of the 1st of the Month for the last 5 years. How can i do that ?? I have a select statement:
select convert(varchar(10), dateadd(mm,Datediff(mm,0,getdate()),0),111) as StartDate
but i am not sure how to get a list for every month.
with dates
as (
select dateadd(month, datediff(month, 0, getdate()), 0) as date
union all
select dateadd(month, - 1, date)
from dates
)
select top 60 *
from dates
with cte as (
select DATEFROMPARTS ( datepart(yyyy,getdate()), datepart(mm,getdate()), 1 ) as startdate
union all
select dateadd(month,-1,startdate) from dates
where datediff(year,startdate,getdate()) <> 5 )
select CONVERT ( varchar(12), startdate , 107 ) from cte;
In my table, I have some ID's associated with each Date. How do I count the number of ID's associated with a date ? The date column is in datetime, but I want to group by date and not date time.
Expected output -
11/14/2013, 30 counts
11/15/2013, 400 counts
etc
I am using SQL server 2000.
Group by DATEADD(day, DATEDIFF(day, 0, DateTimeCol), 0) which truncates the time to midnight.
SELECT Date = DATEADD(day, DATEDIFF(day, 0, DateTimeCol), 0)
, COUNT = COUNT(*)
FROM dbo.TableName
GROUP BY DATEADD(day, DATEDIFF(day, 0, DateTimeCol), 0)
I have a column called Work Done where on daily basis some amount of work is caarried out. It has columns
Id, VoucherDt, Amount
Now my report has scenario to print the sum of amount till date of the month. For example if Current date is 3rd September 2013 then the query will pick all records of 1st,2nd and 3rd Sept and return a sum of that.
I am able to get the first date of the current month. and I am using the following condition
VoucherDt between FirstDate and GetDate() but it doesnot givign the desired result. So kindly suggest me the proper where condition.
SELECT SUM(AMOUNT) SUM_AMOUNT FROM <table>
WHERE VoucherDt >= DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)
AND VoucherDt < DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 1)
I think that there might be a better solution but this should work:
where YEAR(VoucherDt) = YEAR(CURRENT_TIMESTAMP)
and MONTH(VoucherDt) = MONTH(CURRENT_TIMESTAMP)
and DAY(VoucherDt) <= DAY(CURRENT_TIMESTAMP)
Try to calc the number of months from the first date that you can store in a datetime an your target dates.
SELECT SUM(amount)
FROM
(
SELECT 100000 AS amount, '2013-09-03' AS dt
UNION ALL SELECT 10000, '2013-09-02'
UNION ALL SELECT 1000, '2013-09-01'
UNION ALL SELECT 100, '2013-08-02'
UNION ALL SELECT 10, '2013-01-31'
UNION ALL SELECT 2, '2012-09-03'
UNION ALL SELECT 2, '2012-09-02'
UNION ALL SELECT 1, '2012-09-01'
) SourceData
WHERE DATEDIFF(m, '1900-1-1', GETDATE()) = DATEDIFF(m, '1900-1-1', SourceData.dt)
I was trying to get the last three Thursday dates based on the date which I gave. I got it right if i insert a Tuesdays date but does not work for a Friday date. What i did was:
select dt from (select *,ROW_NUMBER() over (order by(d.dt)) as rn from
(SELECT Top 4 CONVERT(varchar, DATEADD(Month, -1, '5/29/2012')+number,101) as dt
FROM master..spt_values WHERE TYPE ='p' AND
DATEDIFF(d,DATEADD(Month, -1, '5/29/2012'),'5/29/2012') >= number
AND DATENAME(w,DATEADD(Month, -1, '5/29/2012')+number) = 'Thursday') as d )
as nw where nw.rn>1
and the output I get is 5/10/2012, 5/17/2012, 5/24/2012.
But when I change the date to 5/18/2012 the output i get is
04/26/2012, 05/03/2012, 05/10/2012
But it should be 05/3, 05/10, 05/17.....What is wrong or is there any other method to do this?
Just replace DATEADD(Month, -1, with DATEADD(Week, -4, and it starts working for Fridays and Saturdays.
Explanation: If you subtract a Month it usually substracts 30 or 31 days, which adds an extra Thursday if the start date is a Friday, Saturday or (in the 31 days case a Sunday). After that the Top 4 cuts the last Thursday in the list.
Edit, to just select three values:
select * from (select *,ROW_NUMBER() over (order by(d.dt)) as rn from
(
SELECT CONVERT(varchar, DATEADD(Week, -3, '5/25/2012')+number,101) as dt
FROM master..spt_values WHERE TYPE ='p'
AND DATEDIFF(d,DATEADD(Week, -3, '5/25/2012'), DATEADD(Day,-1,'5/25/2012')) >= number
AND DATENAME(w,DATEADD(Week, -3, '5/25/2012') + number) = 'Thursday') as d
)
as nw