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 2000 Forums
 SQL Server Development (2000)
 Convert String to Datetime

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-06-27 : 09:18:29
Eve writes "I have to do a date comparison, but one of those dates is a string and the other a datetime. @RptMonth char(7) = '200105 ' stands for May 2001, while BudgetStart datetime = 2000-02-01 00:00:00.000 stands for Feb 1, 2000. I only want dates where BudgetStart <= @RptMonth. I'm having trouble converting/casting @RptMonth to the proper date data type. I use TSQL, MSSQL 7. Thank you for giving this question a shot."

dsdeming

479 Posts

Posted - 2002-06-27 : 09:32:43
This should at least get you started:

WHERE CASE
WHEN RIGHT( RTRIM( @rptMonth ), 2 ) IN( '04', '06', '09', '11' ) -- 30 days in month
THEN BudgetStart <= CONVERT( datetime, RTRIM( @rptMonth ) + '30', 112 )
WHEN RIGHT( RTRIM( @rptMonth ), 2 ) = 2 AND ISDATE( RTRIM( @rptMonth ), 2 ) + '29' ) = 1 -- 29 days in month
THEN BudgetStart <= CONVERT( datetime, RTRIM( @rptMonth ) + '29', 112 )
WHEN RIGHT( RTRIM( @rptMonth ), 2 ) = 2 AND ISDATE( RTRIM( @rptMonth ), 2 ) + '29' ) = 0 -- 28 days in month
THEN BudgetStart <= CONVERT( datetime, RTRIM( @rptMonth ) + '28', 112 )
ELSE -- 31 days in month
THEN BudgetStart <= CONVERT( datetime, RTRIM( @rptMonth ) + '31', 112 )
END


Go to Top of Page
   

- Advertisement -