The problem just illustrates why dates should not be kept as text strings.
Correct calculations with dates can be done easily only when the data type
is DATETIME/SMALLDATETIME.
Here is one way to do the 1 hour subtraction:
SELECT DATEADD(hh,
- 1,
LEFT(d, 8) +
CONVERT(DATETIME, LEFT(RIGHT(d, 4), 2) + ':' +
RIGHT(RIGHT(d, 4), 2),
108))
FROM (SELECT '200802290525' UNION
SELECT '200802290030') AS T(d)
And then it gets even more messy converting back to string format:
SELECT REPLACE(
REPLACE(
REPLACE(
CONVERT(VARCHAR(16),
DATEADD(hh,
- 1,
LEFT(d, 8) +
CONVERT(DATETIME, LEFT(RIGHT(d, 4), 2) + ':' +
RIGHT(RIGHT(d, 4), 2),
108)),
126),
'-', ''),
'T', ''),
':', '')
FROM (SELECT '200802290525' UNION
SELECT '200802290030') AS T(d)
HTH,
Plamen Ratchev
http://www.SQLStudio.com