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
 General SQL Server Forums
 New to SQL Server Programming
 convert number/string to date and then subtract

Author  Topic 

bartbart
Starting Member

3 Posts

Posted - 2010-11-12 : 16:32:32
I am trying to cahng a string to a date and then subtract 7 days form the date for the data. What I have works great until I switch months and then I have to wait 7 days for the data to show up again. the query gives me a number like 20101099 and not 20101101. As stated it all clears up 8 days into the month... How do I fix?
Here is the query:
SELECT convert(varchar, getdate(), 112)as DATE, NBOCDT, NBRIDN, NBMTNO, NBAIQT, NBSIQT, Convert(decimal(6,2),(NBSIQT/NBAIQT ))as "Variance" from MVXJDTA.CPOMAT
Where NBOCDT = convert(varchar, getdate(), 112)-7 and Convert(decimal(6,4),(NBSIQT/NBAIQT )) > 1.1 or Convert(decimal(6,2),(NBSIQT/NBAIQT )) < .9


Thanks in advance

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-12 : 16:35:26
Please show us a complete data example.

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

Subscribe to my blog
Go to Top of Page

TimSman
Posting Yak Master

127 Posts

Posted - 2010-11-12 : 16:35:51
In the code you have, you're converting a date to a string. Is that what you want to do?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-11-12 : 17:34:36
NBOCDT >= convert(varchar, getdate() - 7, 112)


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

bartbart
Starting Member

3 Posts

Posted - 2010-11-15 : 07:55:39
Peso,
Thanks for correcting me. Appears that this change will work for me....
THANKS AGAIN...You are my hero for the day.

Thanks to all for the other responses. With me being new to this and no formal training, I think I have found a place that will help me a ton..

Thanks again.
Go to Top of Page
   

- Advertisement -