将 varchar 数据格式化为某种格式
我需要格式化当前输入的 sql 列中的数据,如下所示:
Z04000002003.7
所需的输出将是:
Z04/000/002/003.7
每当用户输入类似 Z04000002003.7 的数据时。 下次用户打开该记录时,它会自动将其格式化为显示 Z04/000/002/003.7。
I need to format data in a sql column that is currently entered like this:
Z04000002003.7
The desired output of this would be:
Z04/000/002/003.7
Every time a user enters data like this Z04000002003.7. The next time the user opens the record it would have automatically formatted it to display Z04/000/002/003.7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当你说“打开记录”时,到底发生在哪里? 一个网页?
尽可能贴近用户进行格式化 - UI 层。 我不认为这是一个 SQL 问题。
When you say 'open the record' where exactly is that happening? A web page?
Do the formatting as close to the user as you can - UI layer. I don't think this is a SQL problem.
如果你想在 INSERT 或 UPDATE(当字符串进入数据库时)或 SELECT 上插入斜杠,你可以在 TSQL 中用一个有点笨拙的字符串表达式来做到这一点:
等等,但我同意其他受访者的观点,这可能是更好的架构来“更接近用户”执行此类转换(在 UI 中,或者可能在业务逻辑层中,如果这些斜杠实际上是业务逻辑的一部分,但 UI 看起来更可能)。
If you want to insert the slashes on INSERT or UPDATE (when the string gets into the database) or SELECT, you can do that in TSQL with a somewhat clumsy string expression:
and so on, but I agree with other respondents that it may be a better architecture to perform such transformations "closer to the user" (in the UI, or perhaps in a business logic layer if those slashes are in fact part of the business logic, but UI looks likelier).
有几个选项:
1. 脚本将所有行格式从旧标准更新为新标准
2.像n8wrl所说的插入格式
3. 数据返回格式。
A couple of options:
1. Script update all the rows format from old to new standard
2. Like n8wrl said format on insert
3. Format on data return.
数据应在输入数据库时进行格式化。 UI 不必将键入的内容准确发送到数据库。 用户不需要知道您添加了 /s。 如果您无法更改 UI 中的数据条目(这将是我在何处更改它的首选),请编写一个触发器来处理插入或更新数据时的数据,以正确的格式。 确保触发器可以处理多行插入或更新!
The data should be formatted on entry into the database. THe UI doesn't have to send exactly what was typed to the database. The user doesn't need to know you added the /s. If you can't change the data entry in the UI (which would be my first choice of where to change it), then write a trigger to process the data on insert or update to the correct format. Make sure that trigger can handle multitple row inserts or updates!