1

I am converting Access query to SQL Server.

I want to convert below lines to SQL

1. Format (210.6, "Standard")
2. Format (210.6, "#,##0.00")

How do i convert it to SQL query.

I have tried with below, but still not able to find the solution.

For the first query, i found below solution, which is correct

    1. CONVERT(varchar, CAST(tSRO.OutputF11 AS money), 1)

Now, for second query, i do not know what i have to do.

6
  • Share how results should look like Commented Oct 24, 2015 at 12:55
  • Result for first query should be Result: '210.60' and for second query Result: '210.60' Commented Oct 24, 2015 at 12:56
  • And for 1210.6 => 1,210.60? Commented Oct 24, 2015 at 12:56
  • Please avoid crossposting Commented Oct 24, 2015 at 12:56
  • Okay, i will keep this in mind next time Commented Oct 24, 2015 at 12:57

1 Answer 1

2

From SQL Server 2012+ you can use FORMAT:

SELECT FORMAT(210.6, '#,##0.00')  -- 210.60

SELECT FORMAT(1210.6, '#,##0.00')  -- 1,210.60

LiveDemo

SQL Server before 2012:

SELECT REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1210.6),1),'.00','')  -- 1,210.60

LiveDemo2

Warning:

This operation is pure for presentation layer and should be done in application.

Sign up to request clarification or add additional context in comments.

6 Comments

I have tried it in my environment and it is lower version. so that's why it is not working
what if i have a lower version?
Thanks for your help.
Although this solution will work, formatting data for display purposes is better done in the presentation layer rather than SQL. This practice improves database performance and leverages front-end formatting capabilities that are generally more robust.
@lad2025, It doesn't matter of the RDMBS provides formatting capabilities or not. I'm just suggesting the best practice is presentation layer formatting. You are right that it depends but the only overarching reason to do formatting in SQL is initial porting of a legacy application. That may apply here but I just want to caution it's not proper development practice for new development.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.