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)
 Converting to Date

Author  Topic 

oahu9872
Posting Yak Master

112 Posts

Posted - 2008-11-14 : 13:21:16
I have two varchar fields that are for a from date and a to date. I need to get the number of days between the two fields (date_to - date_from).
It says I cannot use a subtract function on a varchar field. The field is formatted YYYYMMDD, So 20040701 for July 1st, 2004. Can I put a convert function around this, and then subtract from another similar field to get the number of days between the two? Thanks.


select (dos_thru - dos_from) as LengthOfStay
from dw_clm_inv01

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-11-14 : 13:41:36
SELECT DATEDIFF(DAY, CAST(Col1 AS DATETIME), CAST(Col2 AS DATETIME))
FROM Table1



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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 23:59:05
This is one of reasons why its told to use proper datatype for your fields.
Go to Top of Page
   

- Advertisement -