Need a SQL Query to find numeric values

  • Hi Experts,

    Can you all help me out to find a query in which i can find all the tables in a particular Database which has numeric values with 3 decimals  present ; situation is that I need to round off the decimal values to two for the numbers which has 3 decimal values.

    Any help on this will be most helpful

    Regards,
    Adil

  • adilahmed1989 - Tuesday, February 13, 2018 12:22 AM

    Hi Experts,

    Can you all help me out to find a query in which i can find all the tables in a particular Database which has numeric values with 3 decimals  present ; situation is that I need to round off the decimal values to two for the numbers which has 3 decimal values.

    Any help on this will be most helpful

    Regards,
    Adil

    Hereby request to kindly help me out with this quickly as we are running under the water and desperate need any kind of solution.
    Sincerely
    AD

  • This was removed by the editor as SPAM

  • Something like this would show you the tables and rows where a column holds data that has at least 3 decimal places: -

    DECLARE @sql NVARCHAR(MAX);

    SELECT @sql = STUFF((SELECT  CHAR(13) + CHAR(10) + 'SELECT ' + CHAR(39) + [o].[name] + CHAR(39) + ' AS [Table_Name], * FROM ['
               + OBJECT_SCHEMA_NAME([o].[object_id]) + '].[' + OBJECT_NAME([o].[object_id]) + '] WHERE ' + [ca].[val] + ';'
            FROM  [sys].[objects] [o]
            CROSS APPLY ( SELECT STUFF((SELECT 'OR (((abs([' + [c].[name] + '])*100) - CONVERT(BIGINT,(abs([' + [c].[name] + '])*100))) <> 0)'
                     FROM  [sys].[columns] [c]
                     INNER JOIN [sys].[types] [t] ON [t].[system_type_id] = [c].[system_type_id]
                     WHERE  [t].[name] IN ( 'real', 'float', 'decimal', 'numeric' )
                        AND [o].[object_id] = [c].[object_id]
                   FOR XML PATH(''),
                      TYPE
    ).[value]('.', 'NVARCHAR(MAX)'), 1, 3, '')
                ) [ca] ( [val] )
            WHERE  [o].[type] = 'U'
               AND [ca].[val] IS NOT NULL
       FOR    XML PATH(''),
             TYPE).[value]('.', 'NVARCHAR(MAX)'), 1, 2, '');

    EXECUTE [sys].[sp_executesql] @sql;


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • adilahmed1989 - Tuesday, February 13, 2018 2:44 AM

    adilahmed1989 - Tuesday, February 13, 2018 12:22 AM

    Hi Experts,

    Can you all help me out to find a query in which i can find all the tables in a particular Database which has numeric values with 3 decimals  present ; situation is that I need to round off the decimal values to two for the numbers which has 3 decimal values.

    Any help on this will be most helpful

    Regards,
    Adil

    Hereby request to kindly help me out with this quickly as we are running under the water and desperate need any kind of solution.
    Sincerely
    AD

    I don't know of a way to validate the precision of the data that is stored in the DB.
    However, the following script will get you a list of Tables and Columns that can store data with a precision of 3 or more.

    SELECT
      SchemaName = s.name
    , TableName = t.name
    , ColumnName = c.name
    , [DataType] = st.name
         + CASE WHEN st.name IN ('decimal', 'numeric')
            THEN ' (' + CONVERT(VARCHAR(10), c.[precision]) + ', ' + CONVERT(VARCHAR(10), c.scale) + ')'
            ELSE ''
          END
    FROM sys.schemas AS s
    INNER JOIN sys.tables AS t
     ON s.schema_id = t.schema_id
    INNER JOIN sys.columns AS c
     ON t.object_id = c.object_id
    INNER JOIN sys.types AS st
     ON c.user_type_id = st.user_type_id
    WHERE t.is_ms_shipped = 0
    AND ((st.name IN ( 'numeric', 'decimal', 'money', 'smallmoney' ) AND c.scale >= 3)
       OR st.name IN ( 'float', 'real' )
       )
    ORDER BY s.name, t.name, c.name;

  • You can look at the following SQL Functions to assist you in transforming your data
    ROUND
    CEILING
    FLOOR

  • adilahmed1989 - Tuesday, February 13, 2018 2:44 AM

    adilahmed1989 - Tuesday, February 13, 2018 12:22 AM

    Hi Experts,

    Can you all help me out to find a query in which i can find all the tables in a particular Database which has numeric values with 3 decimals  present ; situation is that I need to round off the decimal values to two for the numbers which has 3 decimal values.

    Any help on this will be most helpful

    Regards,
    Adil

    Hereby request to kindly help me out with this quickly as we are running under the water and desperate need any kind of solution.
    Sincerely
    AD

    I'd like to see the queries that you'd been working on !

  • adilahmed1989 - Tuesday, February 13, 2018 2:44 AM

    adilahmed1989 - Tuesday, February 13, 2018 12:22 AM

    Hi Experts,

    Can you all help me out to find a query in which i can find all the tables in a particular Database which has numeric values with 3 decimals  present ; situation is that I need to round off the decimal values to two for the numbers which has 3 decimal values.

    Any help on this will be most helpful

    Regards,
    Adil

    Hereby request to kindly help me out with this quickly as we are running under the water and desperate need any kind of solution.
    Sincerely
    AD

    So, you've been offered three answers to your original post, which should at least give you a head start on your project.
    I'm curious. Why would you be in such difficulties and so badly in need of a resolution?
    Thanks.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply