February 16, 2010 at 2:40 pm
Can someone tell me why am I getting error mssg as following
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'IF'.
DECLARE @minutes INT, @word VARCHAR(255),@hours INT, @min-2 INT, @second INT
SET @minutes = ABS(DATEDIFF(MINUTE,'10/16/2009 11:36:47 AM','10/17/2009 4:43:55 PM'))
IF @minutes = 0
SET @word = '00'
ELSE
BEGIN
SET @word = ''
IF @minutes >= (24*60)
set @hours=(RTRIM(@minutes/(24*60))
IF LEN(@hours) < 2
SET @word = @word + '0' + @hours+' :'
IF LEN(@hours) >= 2
SET @word = @word + @hours+' :'
END
February 16, 2010 at 3:15 pm
Did you actually try to figure out what the error message is telling you?
If you can't find anything wrong after the if statement in line 10 the reason might be the error is before the statement. 😉
Strong hint: How many pairs of brackets do you have in your statement "set @hours=(RTRIM(@minutes/(24*60))"
The next strong hint would be given by SSMS by highlighting the corresponding parts of a pair of brackets...
February 16, 2010 at 4:03 pm
You have a problem in this line:
set @hours=(RTRIM(@minutes/(24*60))
Three open-parens, two closes.
Those can be a pain to spot by eye.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
February 16, 2010 at 7:14 pm
LOOKUP_BI-756009 (2/16/2010)
Can someone tell me why am I getting error mssg as followingMsg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'IF'.
DECLARE @minutes INT, @word VARCHAR(255),@hours INT, @min-2 INT, @second INT
SET @minutes = ABS(DATEDIFF(MINUTE,'10/16/2009 11:36:47 AM','10/17/2009 4:43:55 PM'))
IF @minutes = 0
SET @word = '00'
ELSE
BEGIN
SET @word = ''
IF @minutes >= (24*60)
set @hours=(RTRIM(@minutes/(24*60))
IF LEN(@hours) < 2
SET @word = @word + '0' + @hours+' :'
IF LEN(@hours) >= 2
SET @word = @word + @hours+' :'
END
Too much work and guaranteed to be RBAR that way... you're forcing yourself to use a UDF even if you want to do it on a whole table. Try this...
DECLARE @StartDate DATETIME,
@EndDate DATETIME
SELECT @StartDate = '10/16/2009 11:36:47 AM',
@EndDate = '10/17/2009 4:43:55 PM'
SELECT CONVERT(VARCHAR(10),DATEDIFF(hh,0,@EndDate-@StartDate)) --Converts the hours
+ RIGHT(CONVERT(CHAR(8),@EndDate-@StartDate,108),6) --Converts mins & secs
AS [HHHHH:MM:SS]
Neat thing about this is, you don't need a UDF to do a whole table's worth...
SELECT CONVERT(VARCHAR(10),DATEDIFF(hh,0,EndDateCol-StartDateCol)) --Converts the hours
+ RIGHT(CONVERT(CHAR(8),EndDateCol-StartDateCol,108),6) --Converts mins & secs
AS [HHHHH:MM:SS]
FROM dbo.YourTable
With a little effort on your part, you can turn it into a deterministic computed column in your table so you don't have to worry about writing the code correctly each time.
My big question would be... why aren't you doing this type of formatting in the GUI (assuming that you have a GUI)?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply