SQL从科学记数法转换为字符串

时间:2014-10-01 13:24:34

标签: sql sql-server-2005

我正在尝试转换浮动表中的列,以便它们不会显示为科学记数法。我已经尝试了以下但它似乎不起作用:

Update TABLE
SET YTD = STR(YTD,12,2)

没有任何效果,因为我仍然在列中获得2.834566E + 07的值。

以下是完整代码:

declare @app_cur CURSOR
declare @Current_PV_Value float
declare @Current_Tagname varchar(100)
DECLARE @Year INT;
DECLARE @JanDate Datetime
DECLARE @FebDate Datetime
DECLARE @MarDate Datetime
DECLARE @AprDate Datetime
DECLARE @MayDate Datetime
DECLARE @JunDate Datetime
DECLARE @JulDate Datetime
DECLARE @AugDate Datetime
DECLARE @SepDate Datetime
DECLARE @OctDate Datetime
DECLARE @NovDate Datetime
DECLARE @DecDate Datetime
DECLARE @Date SMALLDATETIME;
SET @Date = CONVERT(SMALLDATETIME, CONVERT(CHAR(4), @Year), 112);


SET @Year = 2014;


SET @JanDate = CAST(
                CAST(@Year+1 AS NVARCHAR(4)) + '02' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @FebDate = CAST(
                CAST(@Year+1 AS NVARCHAR(4)) + '03' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @MarDate = CAST(
                CAST(@Year+1 AS NVARCHAR(4)) + '04' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @AprDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '05' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @MayDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '06' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @JunDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '07' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @JulDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '08' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @AugDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '09' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @SepDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '10' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @OctDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '11' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @NovDate = CAST(
                CAST(@Year AS NVARCHAR(4)) + '12' +  '01' + ' 00:05:00'
                AS DATETIME)
SET @DecDate = CAST(
                CAST(@Year+1 AS NVARCHAR(4)) + '01' +  '01' + ' 00:05:00'
                AS DATETIME)
PRINT 'Opening cursor.'
    SET @app_cur = CURSOR FORWARD_ONLY FOR
    SELECT   [Tagname2]
    FROM     PARCS_DB.dbo.tbl_Utilities_PARCS_Totals


    -- Open cursor 1 for iteration.
    OPEN @app_cur

    -- Fetch the first record.
    FETCH NEXT FROM @app_cur INTO   @Current_Tagname


    -- Iterate each record.
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
                --clear PV value
                set @Current_PV_Value = null
                --January Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @JanDate
                  AND    [DateTime] <= dateadd(minute,5,@JanDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Jan] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Jan] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --February Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @FebDate
                  AND    [DateTime] <= dateadd(minute,5,@FebDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Feb] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Feb] = 0
                        WHERE   CURRENT OF @app_cur
                END
                Print 'tagname: '+@Current_Tagname
                Print 'value: '+ cast(@Current_PV_Value as varchar(50))
                --March Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @MarDate
                  AND    [DateTime] <= dateadd(minute,5,@MarDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Mar] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Mar] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --April Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @AprDate
                  AND    [DateTime] <= dateadd(minute,5,@AprDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Apr] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Apr] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --May Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @MayDate
                  AND    [DateTime] <= dateadd(minute,5,@MayDate)
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [May] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [May] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --June Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @JunDate
                  AND    [DateTime] <= dateadd(minute,5,@JunDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Jun] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Jun] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --July Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @JulDate
                  AND    [DateTime] <= dateadd(minute,5,@JulDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Jul] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Jul] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --August Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @AugDate
                  AND    [DateTime] <= dateadd(minute,5,@AugDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Aug] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Aug] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --September Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @SepDate
                  AND    [DateTime] <= dateadd(minute,5,@SepDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Sep] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Sep] = 0
                        WHERE   CURRENT OF @app_cur
                END
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Sep] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --October Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @OctDate
                  AND    [DateTime] <= dateadd(minute,5,@OctDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Oct] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Oct] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --November Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @NovDate
                  AND    [DateTime] <= dateadd(minute,5,@NovDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Nov] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Nov] = 0
                        WHERE   CURRENT OF @app_cur
                END
                --December Data
                SELECT   TOP 1 @Current_PV_Value = [Value]
                FROM     [Runtime].[dbo].[History]
                WHERE    [TagName] = @Current_Tagname
                  AND    wwRetrievalMode = 'Delta'
                  AND    wwVersion = 'Latest'
                  AND    [DateTime] >= @DecDate
                  AND    [DateTime] <= dateadd(minute,5,@DecDate) 
                ORDER BY [DateTime] DESC

                if @Current_PV_Value is not null
                Begin
                        -- Update the return set with our phase number value.
                        UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Dec] = @Current_PV_Value
                        WHERE   CURRENT OF @app_cur
                end
                if @Current_PV_Value is null
                BEGIN
                    UPDATE  PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
                        SET     [Dec] = 0
                        WHERE   CURRENT OF @app_cur
                END
        FETCH NEXT FROM @app_cur INTO    @Current_Tagname
    END--End of cursor 1 logic

    -- Close and dispose of the cursor.
    CLOSE @app_cur
    DEALLOCATE @app_cur

--Year to Date Totals
UPDATE PARCS_DB.dbo.tbl_Utilities_PARCS_Totals 

    SET [YTD] = (ISNULL([Jan],0) + ISNULL([Feb],0)+ ISNULL([Mar],0) + ISNULL([Apr],0) + ISNULL([May],0) + ISNULL([Jun],0) + ISNULL([Jul],0) + ISNULL([Aug],0) + ISNULL([Sep],0) + ISNULL([Oct],0) + ISNULL([Nov],0) + ISNULL([Dec],0))

Update PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
SET Apr = round(apr,1),
May = round(may,1),
Jun = round(jun,1),
Jul = round(jul,1),
Aug = round(aug,1),
Sep = round(sep,1),
Oct = round(oct,1),
Nov = round(nov,1),
[Dec] = round(dec,1),
Jan = round(jan,1),
Feb = round(feb,1),
Mar = round(mar,1),
YTD = round(ytd,1)

Update PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
    SET YTD = str(cast(YTD as real))
Select str(cast(YTD as real)) from PARCS_DB.dbo.tbl_Utilities_PARCS_Totals

select * from PARCS_DB.dbo.tbl_Utilities_PARCS_Totals

1 个答案:

答案 0 :(得分:0)

来自MSDN Developer Forums

select str(cast(YTD as real) ) from TABLE

因此,如果上述结果看起来正确:

Update TABLE 
SET YTD = str(cast(YTD as real) )

编辑:事实证明,你需要明确地告诉SQL这是你真正想要做的事情:

BEGIN TRANSACTION UpdateScinot

Update TABLE 
SET YTD = str(cast(YTD as real) )

COMMIT TRANSACTION UpdateScinot
GO