How to subtract 30 days from the current date using SQL Server - sql

I am unable subtract 30 days from the current date and I am a newbie to SQL Server.
This is the data in my column
date
------------------------------
Fri, 14 Nov 2014 23:03:35 GMT
Mon, 03 Nov 2014 15:18:00 GMT
Tue, 11 Nov 2014 01:24:47 GMT
Thu, 06 Nov 2014 19:13:47 GMT
Tue, 04 Nov 2014 12:37:06 GMT
Fri, 1 Nov 2014 00:33:00 GMT
Sat, 5 Nov 2014 01:06:00 GMT
Sun, 16 Nov 2014 06:37:12 GMT
For creating the above column I used varchar(50) and now my problem is I want to display the dates for past 15-20 days from the date column can any one help with this issue ?
update [ how can i display last 7 days dates in order

You can convert it to datetime, and then use DATEADD(DAY, -30, date).
See here.
edit
I suspect many people are finding this question because they want to substract from current date (as is the title of the question, but not what OP intended). The comment of munyul below answers that question more specifically. Since comments are considered ethereal (may be deleted at any given point), I'll repeat it here:
DATEADD(DAY, -30, GETDATE())

Try this:
SELECT GETDATE(), 'Today'
UNION ALL
SELECT DATEADD(DAY, 10, GETDATE()), '10 Days Later'
UNION ALL
SELECT DATEADD(DAY, –10, GETDATE()), '10 Days Earlier'
UNION ALL
SELECT DATEADD(MONTH, 1, GETDATE()), 'Next Month'
UNION ALL
SELECT DATEADD(MONTH, –1, GETDATE()), 'Previous Month'
UNION ALL
SELECT DATEADD(YEAR, 1, GETDATE()), 'Next Year'
UNION ALL
SELECT DATEADD(YEAR, –1, GETDATE()), 'Previous Year'
Result Set:
———————– —————
2011-05-20 21:11:42.390 Today
2011-05-30 21:11:42.390 10 Days Later
2011-05-10 21:11:42.390 10 Days Earlier
2011-06-20 21:11:42.390 Next Month
2011-04-20 21:11:42.390 Previous Month
2012-05-20 21:11:42.390 Next Year
2010-05-20 21:11:42.390 Previous Year

TRY THIS:
Cast your VARCHAR value to DATETIME and add -30 for subtraction. Also, In sql-server the format Fri, 14 Nov 2014 23:03:35 GMT was not converted to DATETIME. Try substring for it:
SELECT DATEADD(dd, -30,
CAST(SUBSTRING ('Fri, 14 Nov 2014 23:03:35 GMT', 6, 21)
AS DATETIME))

SELECT DATEADD(day,-30,date) AS before30d
FROM...
But it is strongly recommended to keep date in datetime column, not varchar.

Related

how to use datediff to group Monday to Sunday when first date equals Sunday

Recognizing this is a common question, answered many times, I've not yet found a solution for my query.
My SQLServer ##DateFirst = 7 (Sunday) and I'm trying to Group a period of Invoice [Date]s by Week Starting Monday to Sunday.
The Query works fine but Sunday Dates are being grouped into the following Mondays Group. I understand why this is, but can't find a solution. I can't create Functions, so these methods don't help either.
SELECT
DATEADD(WEEK, DATEDIFF(WEEK,0, [DATE]), 0) AS 'DATE GROUP'
,[BRANCH]
,[SUNDTYPE]
,COUNT([INV_NUM]) AS 'Number of Sundrys'
,SUM([COST]) AS 'Sundry Rev'
FROM Invoice_Table
WHERE [DATE] BETWEEN '2022-03-07 00:00:00.000' AND '2022-03-13 00:00:00.000'
GROUP BY DATEADD(WEEK, DATEDIFF(WEEK,0, [DATE]), 0), SUNDTYPE, BRANCH
SQL Server is 2012
DATEADD and DATEDIFF to group all dates to the Monday of every week, Expecting Monday to Sunday Dates to all be Grouped together
You should be able to fake it out by shifting the date in question BACK 1 day. This way, what you are LOOKING FOR is
Mon Tue Wed Thu Fri Sat Sun
But the WEEK functionality is based on
Sun Mon Tue Wed Thu Fri Sat
So if your data based on this week for example is
Date IS What you ARE getting What you want
Fri Apr 1 Week X Week X
Sat Apr 2 Week X Week X
Sun Apr 3 Week Y Week X
Mon Apr 4 Week Y Week Y
Tue Apr 5 Week Y Week Y
Just subtract 1 day from the date in question that is being checked against the week function(), thus rolling all days back 1 makes Monday look like Sunday which is your correct break point, and Sunday rolls back to the previous week's Saturday.
DATEADD(WEEK, DATEDIFF(WEEK,0, dateadd( day, -1, [Date])), 0) AS 'DATE GROUP'
Then you can just re-add the date back 1 day to re-calibrate to Monday for display purposes.

How to get data from Jan to last month in current month

I have a requirement where I need the data from Jan to last month, so for Dec 2017 I want the data from Jan 2017 - Nov 2017 , and in Jan 2018 I want the data from jan 2017 - Dec 2017, in feb 2018 I want the data from 1st Jan 2018- 31st Jan 2018, in March I want Jan 2018 - Feb 2018 and so on.
Below is my code:
(case when (DateFilled) between cast (DATEADD(YEAR, DATEDIFF(YEAR, '19000101', '2018-01-05'), '19000101') as datetime)
and cast (DATEADD(D, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', '2018-01-05'), '19000101')) as datetime)
THEN 1 else 0 end) as numeric(10,2) [TillLastMonth]
(Datefilled is the column with datatime datatype)
Any help is appreciated!!
Thanks

SQL date convert from 20 Dec 2016 to 20 December 2016

current SQL to get 20 Dec 2016 is:
Convert(char(11), getdate(), 13) DATE,
SELECT FORMAT(cast('20 DEC 2016' as date),'dd MMMM yyyy')

Changing date format for PostgreSQL query result

I have following query
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date
Now listDate value can be like
select substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '............$') as v_end_date,
substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '^............') as v_start_date
Above query results in
V_END_DATE V_START_DATE
26 Jan 2014 06 Jan 2014
Now I need to have v_end_date and v_start_date format like yyyy-mm-dd and like
Mon 06 Jan 2014.
Convert your string to an actual date with to_date() and use to_char() to get pretty much any format you like.
Demo:
SELECT to_char(day, 'YYYY-MM-DD') AS format1
, to_char(day, 'Dy DD Mon YYYY') AS format2
FROM (SELECT to_date('26 Jan 2014', 'DD Mon YYYY') AS day) sub

SQL Server : group by week off by 1 day

Using the syntax
dateadd(week, datediff(week, 0, CURRENT_TIMESTAMP),0)
to group data by week it appears that the last day of the week gets put into the following week.
In a simple example
print dateadd(week, datediff(week, 0, '27 jan 2012 00:00'),0)
print dateadd(week, datediff(week, 0, '28 jan 2012 00:00'),0)
print dateadd(week, datediff(week, 0, '29 jan 2012 00:00'),0)
print dateadd(week, datediff(week, 0, '30 jan 2012 00:00'),0)
print dateadd(week, datediff(week, 0, '31 jan 2012 00:00'),0)
I would expect to return
Jan 23 2012 12:00AM
Jan 23 2012 12:00AM
Jan 23 2012 12:00AM
Jan 30 2012 12:00AM
Jan 30 2012 12:00AM
or
Jan 22 2012 12:00AM
Jan 22 2012 12:00AM
Jan 29 2012 12:00AM
Jan 29 2012 12:00AM
Jan 29 2012 12:00AM
depending on DATEFIRST settings I guess
however it returns
Jan 23 2012 12:00AM
Jan 23 2012 12:00AM
Jan 30 2012 12:00AM
Jan 30 2012 12:00AM
Jan 30 2012 12:00AM
i.e. the 29 January is in the week of 30th January not 23rd January or 30th January as expected.
I'm clearly missing something but can't think what it might be.
This is correct.
No matter what ##DATEFIRST is, there is always the same number of whole weeks between now and 01 Jan 1900. As ##DATEFIRST changes, it affects both "start" and "end" week.
DATEADD then just adds 7 day periods because it ignores ##DATEFIRST
You can force it by choosing the DATEADD base
Example: 01 Jan 1900 is a Monday.
You want Sunday? Then base to 31 Dec 1899
print dateadd(week, datediff(week, 0, '27 jan 2012 00:00'),-1)
print dateadd(week, datediff(week, 0, '28 jan 2012 00:00'),-1)
print dateadd(week, datediff(week, 0, '29 jan 2012 00:00'),-1)
print dateadd(week, datediff(week, 0, '30 jan 2012 00:00'),-1)
print dateadd(week, datediff(week, 0, '31 jan 2012 00:00'),-1)
You want Tuesday? Then base to 02 Jan 1900
print dateadd(week, datediff(week, 0, '27 jan 2012 00:00'),1)
print dateadd(week, datediff(week, 0, '28 jan 2012 00:00'),1)
print dateadd(week, datediff(week, 0, '29 jan 2012 00:00'),1)
print dateadd(week, datediff(week, 0, '30 jan 2012 00:00'),1)
print dateadd(week, datediff(week, 0, '31 jan 2012 00:00'),1)
This should convert the date to the first day of the week for the purposes of grouping
set datefirst 1
select dateadd(day, -1 * (datepart(weekday, getdate()) - 1), getdate())
Note that the set datefirst 1 sets the first day of the week as Monday. You can alter this statement to change the first day of the week to fit to your expectations (see http://msdn.microsoft.com/en-us/library/ms181598.aspx)
DATEDIFF doesn't respect the DATEFIRST setting.
Here's a similar question, with a potential workaround:
Is it possible to set start of week for T-SQL DATEDIFF function?