How to store date in dd-mon-yyyy format in SQL Server?

╄→гoц情女王★ 提交于 2021-02-17 06:00:07

问题


The below works for displaying the date, but when writing to the database [in a datetime column] it seems to be getting converted to the default format.

REPLACE(CONVERT(VARCHAR(11),CONVERT(DATETIME,DATE_FIELD),106),' ','-')

回答1:


Date/time values in the database are stored in a binary format. They are not stored as strings. This is the right way to store them.

If you want to fetch the data in a particular format, then use the formula when you retrieve them. Or, you can add a computed column:

alter table t
    add date_field_ddmonyyyy as (REPLACE(CONVERT(VARCHAR(11), CONVERT(DATETIME,DATE_FIELD), 106), ' ', '-'));


来源:https://stackoverflow.com/questions/40033754/how-to-store-date-in-dd-mon-yyyy-format-in-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!