I want to select all last 3 month dates from the table in SQL Server.
Data is like this:
Sunday 20-05-2012
Sunday 27-05-2012
Sunday 10-06-2012
Sunday 24-06-2012
Sunday 08-07-2012
Sunday 22-07-2012
Sunday 12-08-2012
Sunday 19-08-2012
Sunday 09-09-2012
Sunday 16-09-2012
Saving date column as varchar with day of week - bad idea. You can't write faster query, because you must convert all of time your field for using it in where clause. Also your query can't use indexes of date_column and each query will be use scan index.
With datetime column, query should be:
select date_column
from table_name
where date_column between dateadd(m, -3, getdate()) and getdate()
Try using this query:
select date_column from table_name
where datepart(m,date_column) > datepart(m,getdate())-3 and datepart(yy,date_column) >= datepart(yy,getdate())
Related
I have table with two datetime columns in SQL Server 2000, I want to update only day and month in column_a with day and month in column_b except year.
For example:
column_a
----------
1/2/2009
1/2/2002
1/2/2016
3/1//1998
11/12/1987
column_b
---------
31/12/2015
11/10/2005
27/6/2017
31/12/2010
31/12/2011
Desired results:
31/12/2009
11/10/2002
27/6/2016
31/12/1998
31/12/1987
Thank you for your help.
Using date literals should work in SQL Server 2000, style 112 returns the date in YYYYMMDD so take the YYYY of one and add to MMDD of the other and convert that to datetime.
update tablex
set columnb = convert(datetime(left(convert(varchar, column_a, 112), 4) +
right(convert(varchar, column_b, 112), 4), 112)
It's a great deal easier in later versions. SQL Server 2000 is dead to me.
Use DATEFROMPARTS ( year, month, day )
update tablex
set columnb = datefromparts(year(column_a),month(column_b), day(column_b))
I´m writing a query where i get the last month, but with the time in zeros (if today is 2013-05-21 then i want to get 2013-04-21 00:00:00.000).
So I tried:
select (dateadd(month,datediff(month,(0),getdate())-1,(0)));
But I get the first day of the previous month.
Then I tried:
select dateadd(month, -1, GETDATE());
I get the right day, but I also get the current time (2013-04-21 11:41:31.090), and I want the time in zeros.
So how should my query be in order to get something like: 2013-04-21 00:00:00.000
Thanks in advance.
In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd.
SELECT DateAdd(month, -1, Convert(date, GetDate()));
This will return a date data type. To force it to be datetime again, you can simply add one more Convert:
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
You may not need the explicit conversion to datetime, though.
Note: "One month ago from today" could be defined in many different ways. The way it works in SQL server is to return the day from the previous month that is the closest to the same day number as the current month. This means that the result of this expression when run on March 31 will be February 28. So, you may not get expected results in certain scenarios if you don't think clearly about the ramifications of this, such as if you performed the one-month calculation multiple times, expecting to get the same day in a different month (such as doing March -> February -> January).
See a live demo at SQL Fiddle
The demo shows the values and resulting data types of each expression.
Try like this..
select Cast(Cast(dateadd(month, -1, GETDATE()) as Date) as Datetime);
You can use this , it's pretty simple and worked for me -
SELECT SUM( amount ) AS total FROM expenses WHERE MONTH( date ) = MONTH( curdate() ) -1
To get the previous month start date and end date
DECLARE #StartDate date;
DECLARE #EndDate date;
select #StartDate= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
select #EndDate= DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
Here are many common dates you may need to pull with logic
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) -- Today Midnight
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1) -- Yesterday Midnight
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- First of Last Month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- Same Day Last Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) -- Last of Last Month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) -- First of this month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- Last of this month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- First of next month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- Same Day Next Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)) -- Last of next month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+12,0)) -- Last of prior month one year from now
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(DAY, 13-(##DATEFIRST + (DATEPART(WEEKDAY,GETDATE()) %7)), GETDATE())),0) -- Next Friday Midnight
I have a sample data set below:
Date
01/01/2010
01/02/2010
01/03/2010
Running the query below gives:
SELECT
DATEPART (MONTH, Date) AS MONTH
FROM MYTABLE
OUTPUT:
MONTH
1
I would like to the output as mm/dd/yyyy format as below.
MONTH
01/01/2010
Could someone please look into it?
Thanks a bunch!
If you want to truncate date to a month, you might use:
select dateadd(m, datediff(m, 0, getdate()), 0)
Put your date column instead of getdate().
If you are running the latest SQL Server 2012, you can use datefromparts function to make a "first of the month" date, like this:
SELECT
DATEFROMPARTS(YEAR(Date), MONTH(Date), 1) AS FIRST_OF_THE_MONTH
FROM MYTABLE
In my db, I have a column, 'Transaction Date' with datetime datatype. For instance, '2011-05-31 00:00:00.000'.
I would like to create a SQL Query by selecting data with whereby the 'Transaction Date' column date is one month before the #InputDate.
I have tried with...
DATEADD(MONTH,-1,#InputDate) and it returns '30-May-2011', which is not what i want!
I want the value returns will always be the last day of the month like '31-May-2011'
Use the following scripts:
Last Day of Previous Month:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) LastDay_PreviousMonth
Last Day of Current Month:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) LastDay_CurrentMonth
Last Day of Next Month:
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)) LastDay_NextMonth
Last Day of Any Month and Year:
DECLARE #dtDate DATETIME
SET #dtDate = '8/18/2007'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#dtDate)+1,0))
LastDay_AnyMonth
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
GETDATE() can be replaced by your input date
Last day of same month
SELECT DATEADD(m, DATEDIFF(m, -1, '2011-05-31'), -1)
Last day of last month
SELECT DATEADD(m, DATEDIFF(m, 0, '2011-05-31'), -1)
I think you're asking, given any date, for the last day of the previous month?
If so, the following works (where CURRENT_TIMESTAMP is being used as the date to search from, '20010101' and '20001231' are constants):
select DATEADD(month,DATEDIFF(month,'20010101',CURRENT_TIMESTAMP),'20001231')
It works because the relationship between the two constant dates is that, compared to '20010101', '20001231' was the last date of the month before.
Since SQL Server 2012 you can use the EOMONTH built-in function to get the last day of a month.
So, to get the last day of the previous month you can use this query:
SELECT EOMONTH(GETDATE(), -1)
Where instead of GETDATE() you can put your Date var.
SELECT EOMONTH(#InputDate, -1)
How do I exclude values in a DateTime column that are Saturdays or Sundays?
For example, given the following data:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-28 09:00:00' -- Saturday
'2009-11-29 09:00:00' -- Sunday
'2009-11-30 09:00:00' -- Monday
this is the result I'm looking for:
date_created
'2009-11-26 09:00:00' -- Thursday
'2009-11-27 09:00:00' -- Friday
'2009-11-30 09:00:00' -- Monday
Thanks!
When dealing with day-of-week calculations, it's important to take account of the current DATEFIRST settings. This query will always correctly exclude weekend days, using ##DATEFIRST to account for any possible setting for the first day of the week.
SELECT *
FROM your_table
WHERE ((DATEPART(dw, date_created) + ##DATEFIRST) % 7) NOT IN (0, 1)
SELECT date_created
FROM your_table
WHERE DATENAME(dw, date_created) NOT IN ('Saturday', 'Sunday')
Assuming you're using SQL Server, use DATEPART with dw:
SELECT date_created
FROM your_table
WHERE DATEPART(dw, date_created) NOT IN (1, 7);
EDIT: I should point out that the actual numeric value returned by DATEPART(dw) is determined by the value set by using SET DATEFIRST:
http://msdn.microsoft.com/en-us/library/ms181598.aspx
Try the DATENAME() function:
select [date_created]
from table
where DATENAME(WEEKDAY, [date_created]) <> 'Saturday'
and DATENAME(WEEKDAY, [date_created]) <> 'Sunday'
The answer depends on your server's week-start set up, so it's either
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (7,1)
if Sunday is the first day of the week for your server
or
SELECT [date_created] FROM table WHERE DATEPART(w,[date_created]) NOT IN (6,7)
if Monday is the first day of the week for your server
Comment if you've got any questions :-)
Calculate Leave working days in a table column as a default value--updated
If you are using SQL here is the query which can help you: http://gallery.technet.microsoft.com/Calculate...
Try this code
select (DATEDIFF(DD,'2014-08-01','2014-08-14')+1)- (DATEDIFF(WK,'2014-08-01','2014-08-14')* 2)