July 21, 2016 at 3:33 am
Set Date based on Month
Hi,
I want to set a date to be always two years behind but one want this changing if it falls on the first of April (01-APR)
It also needs to be in DD-MMM-YYYY format.
declare @date as date
Set @date = '01-APR-2016'
--GETDATE()
Select replace(upper(convert(varchar,@date,106)),' ','-') as NewDate
So this example i want the NewDate to be '01-APR-2014'
Thanks
July 21, 2016 at 3:42 am
Dates have no format when they are stored in the database: the date format is something that helps human beings interpreting the date, but in the database they are stored in a format-agnostic way.
If you need to format a date in a particular way, do that from the client application.
That said, what you need to do to obtain two years' ago today is:
SELECT DATEADD(year, -2, GETDATE())
-- Gianluca Sartori
July 21, 2016 at 3:48 am
Hi,
Okay thanks, but how do I fixed the date to be '01-APR' and then the relevant year?
Thanks
July 21, 2016 at 4:12 am
This will return 1st April from the year two years before the given date. If you need the date presented in a particular way, do that in the application. If you must use T-SQL, use the CONVERT function and the applicable style number.SELECT DATEADD(month,12*(DATEDIFF(year,'19000101',GETDATE())-2)+3,'19000101')
John
July 21, 2016 at 5:01 am
Quick question, if the date is earlier than the 1st of April then should it return the year - 2 or -3?, i.e.
a) 2016-01-01 ==> 2014-04-01 (21 months)
b) 2016-01-01 ==> 2013-04-01 (33 months)
😎
July 21, 2016 at 7:56 am
John Mitchell-245523 (7/21/2016)
This will return 1st April from the year two years before the given date. If you need the date presented in a particular way, do that in the application. If you must use T-SQL, use the CONVERT function and the applicable style number.SELECT DATEADD(month,12*(DATEDIFF(year,'19000101',GETDATE())-2)+3,'19000101')
John
Similar logic, shorter version.
SELECT DATEADD(month,12*(YEAR(GETDATE())-1902)+3,0)
July 21, 2016 at 7:59 am
And yet another option:
SELECT DATEADD(MM, DATEDIFF(MM, '19020401', GETDATE()), 0)
July 21, 2016 at 8:23 am
Yes! I like that. I think I prefer going with explicit dates rather than integers - it makes it easier to see at a glance what it's doing. But like I said, that's just a personal preference.
SELECT DATEADD(MM, DATEDIFF(MM, '19020401', GETDATE()), '19000101')
John
July 21, 2016 at 8:52 am
spaghettidba (7/21/2016)
Dates have no format when they are stored in the database
Dates definitively have a format when they are stored in the database.
This format partly determines what you can do and what not. And the format differs between SQL-server, Oracle, Excell and a number of other systems.
The format in SQL-server database is not suetable to be presented to humans, so needs to be presented in a more readable format.
For presentation formats Google on 'CAST AND CONVERT'
SQL code example:
declare @Adate datetime = '20140401'
SET LANGUAGE english -- same as us.
select replace(CONVERT(varchar(30), @Adate, 106),' ','-') -- 01-Apr-2014
SET LANGUAGE us_english
select CONVERT(varchar(30), @Adate, 106)
SET LANGUAGE british
select CONVERT(varchar(30), @Adate, 106)
SET LANGUAGE dutch
select CONVERT(varchar(30), @Adate, 106)
Remarks:
106 does not supply the '-', so here a replace is used.
This is regional settings and language dependend.
See CAST and CONVERT for other options than 106.
See the other replies for the calculation options.
See
For detailed information about date time storage and formats.
Ben
Funfact :
February 30 was a real date in Sweden in 1712.
July 21, 2016 at 8:55 am
Oops
July 22, 2016 at 1:16 am
Luis Cazares (7/21/2016)
Similar logic, shorter version.
SELECT DATEADD(month,12*(YEAR(GETDATE())-1902)+3,0)
This is a bit 'short' I would guess:
SELECT DATEADD(month,12*(YEAR('20160401')-1902)+3,0) -- Result 2014-04-01 00:00:00.000
SELECT DATEADD(month,12*(YEAR('20160331')-1902)+3,0) -- Oops ! 2014-04-01 00:00:00.000 <--- This is not correct
Ben
July 22, 2016 at 7:45 am
ben.brugman (7/22/2016)
Luis Cazares (7/21/2016)
Similar logic, shorter version.
SELECT DATEADD(month,12*(YEAR(GETDATE())-1902)+3,0)
This is a bit 'short' I would guess:
SELECT DATEADD(month,12*(YEAR('20160401')-1902)+3,0) -- Result 2014-04-01 00:00:00.000
SELECT DATEADD(month,12*(YEAR('20160331')-1902)+3,0) -- Oops ! 2014-04-01 00:00:00.000 <--- This is not correct
Ben
Please explain why is it not correct? It's working as intended.
July 22, 2016 at 8:13 am
Eirikur Eiriksson (7/21/2016)
Quick question, if the date is earlier than the 1st of April then should it return the year - 2 or -3?, i.e.a) 2016-01-01 ==> 2014-04-01 (21 months)
b) 2016-01-01 ==> 2013-04-01 (33 months)
😎
Any reason why you don't answer this question? Not providing the necessary information does not help in any way.
😎
August 1, 2016 at 5:47 am
Luis Cazares (7/22/2016)
ben.brugman (7/22/2016)
Luis Cazares (7/21/2016)
Similar logic, shorter version.
SELECT DATEADD(month,12*(YEAR(GETDATE())-1902)+3,0)
This is a bit 'short' I would guess:
SELECT DATEADD(month,12*(YEAR('20160401')-1902)+3,0) -- Result 2014-04-01 00:00:00.000
SELECT DATEADD(month,12*(YEAR('20160331')-1902)+3,0) -- Oops ! 2014-04-01 00:00:00.000 <--- This is not correct
Ben
Please explain why is it not correct? It's working as intended.
SQL_Kills (7/21/2016)
I want to set a date to be always two years behind but one want this changing if it falls on the first of April (01-APR)
From this I understood that on the first off April the data should change.
So '20160331' would become 2013-04-01 (Two years and something, but less than 3 years)
And '20160401' would become 2014-04-01 (Two years exactly)
This is what I understood from the TP. But I might be wrong, the question was and is not totally clear.
Ben
August 1, 2016 at 10:27 pm
ben.brugman (7/21/2016)
spaghettidba (7/21/2016)
Dates have no format when they are stored in the databaseDates definitively have a format when they are stored in the database.
Dates do not have format stored in the database.
Full stop.
End of story.
All date-related data types store the dates as binary strings, no place is reserved for formatting.
_____________
Code for TallyGenerator
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply