get last previous month records [duplicate] - sql

This question already has answers here:
Get the records of last month in SQL server
(23 answers)
Closed 8 years ago.
sql 2005 server
get previous month records
Date product
24-05-2014 ball
25-05-2014 bat
01-06-2014 hat
i need
Date Product
24-05-2014 ball
25-05-2014 bat
declare #ex datetime
set #ex '06-01-2014'
select * from tabl where DATENAME(m,DATEADD(m,0,Date)) =DATENAME(m, DATEADD(m,0, #ex))- it works
select * from tabl where DATENAME(m,DATEADD(m,0,Date)) =DATENAME(m, DATEADD(m,-1,#ex))-not works

My sample code (tested on 2008). I don't know are YEAR and MOTH function in 2005 if not you need to use some string function to extract date / month part from datetime converted to string
declare #ex datetime = '2014-01-01'
declare #prev_year int
declare #prev_month int
set #prev_year = year(dateadd(month, -1, #ex))
set #prev_month = month(dateadd(month, -1, #ex))
select * from tabl
where year(Date) = #prev_year and month(Date) = #prev_month

You're using dd-MM-yyyy format for your dates, which is a varchar in SQL Server.
Therefor, you must use CONVERT :
declare #ex varchar(10)
set #ex = '06-01-2014'
SELECT DATENAME(m,DATEADD(m,0,GETDATE())),
DATENAME(m, DATEADD(m,-1,CONVERT(date, #ex, 103)));
This yields results:
June | May
I think you can figure out your solution from here.
Note: If you use declare #ex datetime , your results will yield June | December

Related

sql datetime. how to set the month [duplicate]

This question already has answers here:
Replace year in datetime date
(3 answers)
Closed 3 years ago.
DECLARE #DateMin AS datetime = '2019-01-05 00:00:00';
DECLARE #PrmMois AS tinyint = 4;
How do I replace the month by 04 ?
If you are simply looking for replace January with April in your variable, you can use DATEADD method for that as below-
SET #DateMin = DATEADD(MM,3,#DateMin)
If you wants this for any date belongs to any month to be replaced by April, do this below-
SET #DateMin = DATEADD(MM,-MONTH(#DateMin)+4,#DateMin)

I am tring to add a year to a date if it is not before today.

I am tring to add a year to a date if it is not before today. So in the statement below I would like to display 4/20/2018
declare #StartDate datetime
set #StartDate='4/20/2015'
select case when dateadd(year,1,#StartDate)> GETDATE() then
dateadd(year,1,#StartDate) else dATEADD(year,1,(datepart(year,GETDATE()))) end
I've changed the else clause so it works out the difference between the start date and today's date +1 to alter make '4/20/2015' become '4/20/2018'
declare #StartDate date
set #StartDate='4/20/2015'
select
case
when dateadd(year,1,#StartDate)> GETDATE() then dateadd(year,1,#StartDate)
else DATEADD(year,(datepart(year,GETDATE())+1-datepart(year,#StartDate)),#StartDate)
end
Also if you are not using the time part of a datetime data type it's best practice to use date instead as it only requires 3 bytes of storage rather than 8 for datetime.
More on date here: https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql
And a cheatsheet of sql data types here: https://sqlserverrider.files.wordpress.com/2013/04/pic116.png

Add days to a date and insert added date in table for SQL SERVER 2012

#Rdatetime ='2017-04-18 11:49:38.633'
#Date to add = 2
#Output = '2017-04-20 11:49:38.633'
Hi all,
This is my Problem , i am getting Rdatetime and date to be added in another table . i have yo add the date and save it in datetime format
thanks in advance
Use DATEADD built in function :
DECLARE #Rdatetime DATETIME ='2017-04-18 11:49:38.633'
DECLARE #Date INT = 2
DECLARE #Output DATETIME = DATEADD(DAY,#Date,#Rdatetime)
SELECT #Output
You can make use DATEADD function by giving D as the first parameter.Then the number of days you want to add and the date to which you want to add.
SET #Output=DATEADD(D,#Date,#Rdatetime)

How to properly write a SET DATE

I'm creating a function were I provide 3 inputs #FiscalYEar, #StartDate, #EndDate, I also declare a DATE parameter that the year will be -1 of #FiscalYear
SET #fyLowerBound = OCT 1 OF (#FiscalYear - 1)
how do I properly write the SET statement to make it work?
This should work:
DECLARE #FiscalYear INT = 2014,
#fyLowerBound DATE;
SET #fyLowerBound = CAST(CAST((#FiscalYear - 1) AS CHAR(4)) + '1001' AS DATE)
SELECT #fyLowerBound;
This gives 1st October 2013.
The premise being creating a string date in the format yyyyMMdd, in SQL Server this is the only culture insensitive date for DATETIME (yyyy-MM-dd will work for DATE), you then cast that string to a date (or datetime whatever your preference).
So the first step is to turn your integer date into a CHAR(4), you can then create october 1st of this year by concatenating '1001'. You now have a string that will be cast to a date.
SET #fyLowerBound = DATEADD(yy, -1, #FiscalYear)
This will give you a date that's a year less than #FiscalYear. Although I'm not entirely sure this is what you need, given that 'OCT' in your original statement.

SQL . The SP or function should calculate the next date for friday

I need to write a store procedure that will return a next friday date on a given date? for example - if the date is 05/12/2011, then it should return next friday date as 05/13/2011. If you pass, 05/16/2011, then it should return the date is 5/20/2011 (Friday). If you pass friday as the date, then it should return the same date.
I'd make this a scalar UDF as it is easier to consume the output.
CREATE FUNCTION dbo.GetNextFriday(
#D DATETIME
)
RETURNS DATETIME
WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT
AS
BEGIN
RETURN DATEADD(DAY,(13 - (##DATEFIRST + DATEPART(WEEKDAY,#D)))%7,#D)
END
This is for SQL Server 2008. To use in 2005, just change the date fields to your preference for datetime to date conversions. It also assumes you are not changing the default week begin value.
DECLARE #PassedDate date = '5/21/2011';
SELECT DATEADD(DAY,(CASE DATEPART(DW,#PassedDate) WHEN 7 THEN 6 ELSE 6 - DATEPART(DW,#PassedDate) END),#PassedDate);
Similar to the top answer, but without using ##DATEFIRST in the solution:
DECLARE #Today DATETIME = GETDATE(); -- any date
DECLARE #WeekdayIndex SMALLINT = DATEPART(WEEKDAY, #Today);
DECLARE #DaysUntilFriday SMALLINT = (13 - #WeekdayIndex) % 7;
DECLARE #UpcomingFridayDate DATETIME = DATEADD(DAY, #DaysUntilFriday, #Today);
SELECT #UpcomingFridayDate ;
Great solutions here, but I also recommend looking at time tables: you can generate them easily in Analysis server, and they can be indexed to be very fast, giving you lots of easy ways to get next week days (among other things).
You can find out more about them here
In our case, the same solution would be
Select MIN(PK_Date) from Time Where PK_Date > #SomeDate AND Day_Of_Week= 6
And of course when you're doing this for a large recordset, you can also do joins for maximum speed & efficiency.