sql datetime. how to set the month [duplicate] - sql

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)

Related

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

SQL Server 2012 ISDATE() [duplicate]

This question already has answers here:
What is the significance of 1/1/1753 in SQL Server?
(6 answers)
Closed 6 years ago.
The MS doc states that ISDATE()
Returns 1 if the expression is a valid date, time, or datetime value; otherwise, 0
So why is it returning 0 in the example below?
DECLARE #DT VARCHAR(30) = '1/4/1752'
SELECT
ISDATE(#DT),
TRY_CONVERT(DATE, #DT, 101),
TRY_CONVERT(DATETIME, #DT),
TRY_CAST(#DT as DATE),
TRY_CAST(#DT AS DATETIME)
returns
0 1752-01-04 NULL 1752-01-04 NULL
Change the date to 1753 and ...
1 1753-01-04 1753-01-04 00:00:00.000 1753-01-04 1753-01-04 00:00:00.000
select ISDATE('17521231'), ISDATE('17530101') gives
0 1
As explained in the documentation, the earliest datetime value is '1753-01-01'.
I would suggest that you use try_convert() instead. This gives you more flexibility:
try_convert(date, '17521231') is not null
The date data type goes back to year one.

How to get datetime without time with t-sql? [duplicate]

This question already has answers here:
Best approach to remove time part of datetime in SQL Server
(23 answers)
Closed 8 years ago.
Suppose I have variable as:
declare #dt datetime
declare #d datetime
set #dt = getdate()
then I want to get date from #dt and assign it to #d. For example, if #dt='2014-07-02 2:30:22'
#t should be #d = '2014-07-02 00:00:00'
data type for #d should be same datetime, not varchar.
You could cast it to DATE datatype
SELECT CAST(GETDATE() AS DATE)

get last previous month records [duplicate]

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

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.