Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date function

Status
Not open for further replies.

andyfresh

Technical User
Oct 4, 2005
33
GB
Hi

Happy new year to you all,

I cam back after the xmas break to find that one of my date functions isnt working and im unsure why.

This is the code as it is in query Analyzer:

select (convert(char(4),year(getdate())) + '-' + cast(replace(str(Month(DATEADD(DAY,-6,getdate())),2),' ','0') as char(2)) + '-' + cast(replace(str(Day(DATEADD(DAY,-6,getdate())),2),' ','0') as char(2)))

However this returns "2006-12-29" instead of 6 days before the present date.

Can anyone help me with this.

Regards

Andy
 
What is it you are trying to do? Are you simply trying to take a date, and minus 6 days?

Jim
 
you forgot to subtract from the year
2 options
select (convert(char(4),year(DATEADD(DAY,-6,getdate())))
+ '-' + cast(replace(str(Month(DATEADD(DAY,-6,getdate())),2),' ','0') as char(2))
+ '-' + cast(replace(str(Day(DATEADD(DAY,-6,getdate())),2),' ','0') as char(2)))

or
select replace(convert(varchar,dateadd(d,-6,getdate()),102),'.','-')

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Try

Code:
SELECT DateAdd(dd, DateDiff(dd, 6, GetDate()), 0)
 
Hi, Thanks for that. It works perfectly.

Andy
 
Andy, were you responding to me or Denis? I'm not sure if you saw my post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top