DateTime Comparison and Conversion
Posted on 2011-09-13
I have been beating my head against the wall on this one, so I would really like to get some help. I have a system that I am trying to pull data into, but the source data is junk and in a terrible format.
I have a query that converts what they call datetime into a datetime format and would like to compare that against a placeholder that is also datetime. I am running into an error where I cannot compare the two datetimes without running into a 'Syntax error converting datetime from character string." and for the life of me I cannot figure it out.
My query is below:
set @StartTime = '2011-07-07 09:00:00'
set @TableName = 'dbo.a2002opt'
set @sql = '
dateadd(second, 15, cast(substring(datetime, 5, 19) as datetime)) as ''Timestamp'',
casingpress39 as ''CasingPressure'',
staticpress39 as ''StaticPressure'',
flowrate39 as ''Flowrate'',
from ' + @TableName + '
where dateadd(second, 15, cast(substring(datetime, 5, 19) as datetime)) > ' + @StartTime + '
Order by ''Timestamp''
Any assistance would be greatly appreciated. Thank you