Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 A date Problem

Author  Topic 

Trudye
Posting Yak Master

169 Posts

Posted - 2008-08-27 : 12:35:36
I am trying to subtract 90 days from getdate and have the output format be yyyymmdd, But I keep getting
May 29 2008
May 29 2

I am using the following code
Declare @Min_Date as nvarchar (8)
Declare @Min_Date2 as nvarchar (11)


SET @Min_Date2 = DATEADD (day, -90, getdate())
Print @Min_Date2

SET @Min_Date = (SELECT CONVERT(VARCHAR(8), @Min_Date2, 112)
Print @Min_Date

Any words of wisdom?

Thanx,
Trudye

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-27 : 12:37:19
SELECT CONVERT(varchar(8), GETDATE() - 8, 112)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-08-27 : 12:39:28
when converting from varchar to varchar the 3rd parameter has noeffect.

Declare @Min_Date as nvarchar (8)
Declare @Min_Date2 as nvarchar (11)

SET @Min_Date2 = DATEADD (day, -90, getdate())
select @Min_Date2

SELECT @Min_Date = CONVERT(NVARCHAR(8), DATEADD (day, -90, getdate()), 112)
select @Min_Date



_______________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.0 out!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-27 : 12:51:50
quote:
Originally posted by Trudye

I am trying to subtract 90 days from getdate and have the output format be yyyymmdd, But I keep getting
May 29 2008
May 29 2

I am using the following code
Declare @Min_Date as nvarchar (8)
Declare @Min_Date2 as nvarchar (11)


SET @Min_Date2 = DATEADD (day, -90, getdate())
Print @Min_Date2

SET @Min_Date = (SELECT CONVERT(VARCHAR(8), @Min_Date2, 112)
Print @Min_Date

Any words of wisdom?

Thanx,
Trudye



why are you having varchar variables for storing dates?Wont this be enough?

Declare @Min_Date as datetime

SET @Min_Date=DATEADD(d,DATEDIFF(d,0,GETDATE())-90,0)
Go to Top of Page

Trudye
Posting Yak Master

169 Posts

Posted - 2008-08-27 : 12:56:11
Thank you so much, visakh16 (my very knowledable consultant) and spirit1 (I will add your code and your comment to my cheat sheet file), and tkizer thank you also.

Be well,
Trudye
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-27 : 12:58:15
quote:
Originally posted by Trudye

Thank you so much, visakh16 (my very knowledable consultant) and spirit1 (I will add your code and your comment to my cheat sheet file), and tkizer thank you also.

Be well,
Trudye


You're welcome
Go to Top of Page
   

- Advertisement -