简体   繁体   中英

Visual Studio (2017) SQL cannot display % sign

I am aware that there are differences between Oracle SQL and SQL Server. The query runs fine and displays the result well but the issue arises when I want to display it on a pie chart. I am thinking that it might be some Visual Studio restriction.

Here is my SQL statement:

SELECT 
    CAST(ROUND(CAST(COUNT(LoanAcNo) AS FLOAT) / 73 * 100, 1) AS VARCHAR) + '%' AS LoanPercentage,
    LoanType
FROM 
    Loan 
GROUP BY 
    LoanType;

This is how I implemented it:

 public DataSet ReadLoanByLoanType()
 {
        SqlConnection myConn = new SqlConnection(DBConnect);

        StringBuilder sqlStr = new StringBuilder();
        sqlStr.AppendLine("SELECT cast( round( cast ( count(LoanAcNo) as float)  / 73 * 100 , 1 ) as varchar ) + '%' as LoanPercentage , LoanType");
        sqlStr.AppendLine("FROM Loan");
        sqlStr.AppendLine("GROUP BY LoanType");

        SqlDataAdapter da = new SqlDataAdapter(sqlStr.ToString(), myConn);
        DataSet ds = new DataSet();
        da.Fill(ds);

        return ds;
    }

If you want to download values from a database for inclusion in some reporting tool's pie chart, don't turn the value into a string by adding a '%' onto them

Your reports tool will be expecting some numeric value to chart, not a string

SQL:

    SELECT 
      round(count(LoanAcNo)/ 73.0 * 100.0, 1) as LoanPercentage,
      LoanType
    FROM Loan 
    GROUP BY LoanType;

Tip: dividing an integer by a constant number that has a decimal place (eg I divided by 73.0 instead of 73) rather than an integer should make SQLS do the calc using floats - saves you having to cast the int to float/makes the SQL shorter and neater

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM