Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Previous date
Message
From
11/10/2011 09:06:20
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Miscellaneous
Thread ID:
01526025
Message ID:
01526027
Views:
34
>>Hi
>>
>>I'm being passed a date via a csv file.
>>
>>I want to insert into my database the previous October 1st of that date minus 4 years.
>>
>>So if my date is 1st January 2011 I'd want the 1st October 2006
>>
>>Any ideas on how I could get that.
>>
>>Thnaks
>
>Do you just want to subtract 4 years from current date?
>
>If so,
>
>declare @PrevDate date
>set @PrevDate = dateadd(year, -4, cast(CURRENT_TIMESTAMP as DATE)) -- SQL Server 2008+
>
>If you always want October 1, then
>
>select CONVERT(datetime,ltrim(year(CURRENT_TIMESTAMP)-4) + '1001') as PreviousDate
Thanks Naomi but Not quite .

declare @testdate datetime
set @testdate = getdate()-30
select @testdate
select CONVERT(datetime,ltrim(year(@testdate)-4) + '1001')

returns 2007-10-01 00:00:00.000

I would expect the date

2011-09-11 14:01:37.963

minus 4 years

2007-09-11 14:01:37.963

then the previous 1st October is 2006
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform