Hi
I have below code . I want if Closing Balance is -ve then its value should be Red. Can it be done thru HTML in stored procedure
SQL
(SELECT 'A/C Bal.' 'Type',cast(getdate() as date) [Posting Date],'' [Document No],'' [Voucher No.],'' Line_ID,'ABC Bank Ltd' + ' ' +'('+' '+'Closing Bal.'+' '+'='+' '+cast(cast(ISNULL(SUM(S.Debit)-SUM(s.Credit),0.00) +(select sum(debit)-sum(Credit) from jdt1 where jdt1.refdate<cast(DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) as date) ) as numeric(19,2)) as nvarchar(max))+')' 'Particulars',null [Debit(LC)]
,null[Credit(LC)],null [Debit(FC)]
,null[Credit(FC)]
FROM JDT1 S left join oact d on d.AcctCode=s.ShortName
WHERE S.RefDate=cast(DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) as date) )Thanks

Lokesh VarmanRank #374 · 6.8K reputationPosted Aug 15, 2023, 9:48 PM EDT
It seems like you're trying to generate some HTML content within a stored procedure based on certain conditions. While stored procedures are primarily used for database operations and querying data, generating HTML directly within a stored procedure might not be the best approach. Instead, you could retrieve the necessary data from the database using the stored procedure and then use HTML in your application code (e.g., in a web page or a reporting tool) to apply styling based on the conditions you mentioned.
Here's a simplified example of how you might structure your code:
DECLARE @ClosingBalance numeric(19, 2) -- Calculate Closing Balance SELECT @ClosingBalance = ISNULL(SUM(S.Debit) - SUM(S.Credit), 0.00) + (SELECT SUM(Debit) - SUM(Credit) FROM JDT1 WHERE JDT1.RefDate ' IF @ClosingBalance ' ELSE SET @HtmlOutput = @HtmlOutput + '' SET @HtmlOutput = @HtmlOutput + 'A/C Bal.' -- Add more table cells and data here SET @HtmlOutput = @HtmlOutput + '' -- Return the HTML output SELECT @HtmlOutput AS HtmlContentIn this example, the SQL code calculates the closing balance and generates HTML markup with conditional styling based on the closing balance. However, it's important to note that embedding HTML directly within a stored procedure is not a common practice. Typically, the data would be retrieved through a stored procedure and then formatted and displayed using application code (e.g., PHP, ASP.NET, JavaScript, etc.) to handle the presentation layer and styling.
Saravanan GanesanRank #153 · 15.1K reputationPosted Aug 16, 2023, 1:38 PM EDT
In HTML and within a stored procedure, you can't directly apply conditional formatting like changing the color based on a value. HTML alone doesn't provide this capability. However, you can achieve this using CSS or JavaScript in conjunction with your HTML output.
In your scenario, it's not recommended to handle such styling within a stored procedure; it's better suited for the presentation layer. You could generate the HTML output from your stored procedure and then, in your web application's frontend, use JavaScript or CSS to identify negative values and apply a red color.
In your HTML generated by the stored procedure:
-100.00This script will find all elements with the class
closing-balance, check their values, and if negative, apply the red color.Remember, separating business logic (like calculation of values) from presentation logic (like styling) is a best practice for maintainable and clean code.