• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

how to subtract date from a number?

Hi,

This is what I would like to do,

Col A is a date in the following format (2005/03/08)  and I would like to subtract this date by the number that's Col B. Then I would like the result to be in a date fromat like Col A. All the columns are varchar.

         Col A - Col B =  Col C
2005/03/08 - 4      =  2005/03/04

TIA,
Frank
0
fcobuzzi
Asked:
fcobuzzi
  • 2
  • 2
1 Solution
 
rafranciscoCommented:
UPDATE YourTable
SET ColC = DATEADD(DD, -CAST(ColB AS INT), CAST(ColA AS DATETIME))
0
 
fcobuzziAuthor Commented:
 
 
That worked great but the date formats are different. My original column has the following format "2005/03/08" and the script that you gave has the following format "Mar  1 2005 12:00AM". I would like to format to be either "Mar  1,2005" or "2005/03/08".  What has to be changed in the script to change the date format?
0
 
rafranciscoCommented:
UPDATE YourTable
SET ColC = CONVERT(VARCHAR(10), DATEADD(DD, -CAST(ColB AS INT), CAST(ColA AS DATETIME), 111)
0
 
fcobuzziAuthor Commented:
Thanks again.
Frank
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now