简体   繁体   中英

Why can I not use format function while Jet-SQL to T-SQL?

I was trying to convert one of my MS Access query containing format to a SQL Server view. I have my view connected to MS Access as linked tables. I was looking at this MS Access to SQL server cheat-sheet to convert Jet-SQL to T-SQL.

The cheat sheet says:

Access: SELECT Format(Value, FormatSpecification) (note: this always returns a string value)

T-SQL: Do not do this in T-SQL; format data at your front-end application or report

I cannot format data at my front end because the SQL Server view is linked as linked tables. I cannot have format function in tables.

The cheat sheet does not provide any explanation on why not to do this in T-SQL.

What is the reason behind not using format when converting Jet-SQL to T-SQL?

Obviously, you can format values in T-SQL using the Format function, which only has minor differences with the Access format function.

Generally, though, you shouldn't.

There are multiple reasons why it's discouraged:

  1. Formatted strings are nearly always larger than unformatted dates/numbers, causing additional overhead when transmitting results
  2. If you format in the application layer, the unformatted value is available to you in the application layer to validate/do calculations with/use for conditional formatting/etc. If you format in the data in the database layer, you can't do this without casting back to a date (which is a really bad practice).
  3. If you want variable formatting based on things like locale settings, it's way easier to format in the application layer.

It's certainly not a limitation of SQL Server. It's just a bad practice to use it.

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