I’m seeking assistance with changing float numbers in a database table so they appear as standard numbers instead of in scientific notation like 2.834566E+07. I attempted to use the following code, but it hasn’t worked:
UPDATE YourTableName
SET YourColumnName = STR(YourColumnName, 12, 2)
Here’s the entire code I’m working with:
declare @cursor CURSOR
declare @CurrentValue float
declare @CurrentRecordID varchar(100)
decimal @Year INT
declare @StartDate Datetime
declare @EndDate Datetime
SET @Year = 2023
SET @StartDate = CAST(
CAST(@Year AS NVARCHAR(4)) + '01' + '01' + ' 12:00:00'
AS DATETIME)
SET @EndDate = CAST(
CAST(@Year AS NVARCHAR(4)) + '12' + '31' + ' 12:00:00'
AS DATETIME)
SET @cursor = CURSOR FORWARD_ONLY FOR
SELECT [RecordID]
FROM YourDatabase.dbo.YourTable
OPEN @cursor
FETCH NEXT FROM @cursor INTO @CurrentRecordID
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT TOP 1 @CurrentValue = [Value]
FROM [YourDatabase].[dbo].[YourValueTable]
WHERE [RecordID] = @CurrentRecordID
AND [DateTime] >= @StartDate AND [DateTime] <= @EndDate
ORDER BY [DateTime] DESC
IF @CurrentValue IS NOT NULL
BEGIN
UPDATE YourDatabase.dbo.YourTable
SET [TheColumn] = @CurrentValue
WHERE CURRENT OF @cursor
END
FETCH NEXT FROM @cursor INTO @CurrentRecordID
END
CLOSE @cursor
DEALLOCATE @cursor
UPDATE YourDatabase.dbo.YourTable
SET Total = (ISNULL(Q1, 0) + ISNULL(Q2, 0) + ISNULL(Q3, 0) + ISNULL(Q4, 0))
UPDATE YourDatabase.dbo.YourTable
SET Total = STR(CAST(Total AS real))
The problem is that my Total column continues to display values in scientific format. What am I doing wrong with the STR function?