I have the following query:
Declare @cnt as int
Declare @int as int
Set @int = 0
Select @cnt =COUNT(Date) FROM tmp
While @int < @cnt
Begin
If (Select Datepart(MM,Start_Time) from dbo.tmp)>='00' and (Select Datepart(MM,Start_Time) from dbo.tmp)<'15'
Set tmp.Start_Int = Datepart(HH,Start_Time)+'00'
else If (Select Datepart(MM,Start_Time) from dbo.tmp)>='15' and (Select Datepart(MM,Start_Time) from dbo.tmp)<'30'
Set tmp.Start_Int = Datepart(HH,Start_Time)+'15'
else If (Select Datepart(MM,Start_Time) from dbo.tmp)>='30' and (Select Datepart(MM,Start_Time) from dbo.tmp)<'45'
Set tmp.Start_Int = Datepart(HH,Start_Time)+'30'
else If (Select Datepart(MM,Start_Time) from dbo.tmp)>='45' and (Select Datepart(MM,Start_Time) from dbo.tmp)<='59'
Set tmp.Start_Int = Datepart(HH,Start_Time)+'45'
end
As you can see, I am trying to update Start_Int to its proper intervals depending on where it falls. Using this, I get an error saying Incorrect syntax near ‘.’. I can assume this query is incorrect, but I cannot find any examples of a while query that uses a select to update a field if a condition is met.
The errors are thrown by the
tmp.Start_Intdeclarations, which I think you are trying to insert values into a database table called tmp.You need to change:
to be something like:
Also you might want to consider using a cursor to read through your data.
EDIT
Could you use the following as a basis?
Returns