日期未转换为yyyy-mm-dd随机记录 [英] Date not converted into yyyy-mm-dd for random record

查看:66
本文介绍了日期未转换为yyyy-mm-dd随机记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的目的是将日期仅以yyyy-mm-dd日期格式保存到NVARCHAR(100)数据类型字段中。



我有一个SQL脚本将所有日期格式转换为yyyy-mm-dd,我有NVARCHAR(100)字段来存储日期。但是一些随机记录以相同的日期格式保存而没有转换。



我尝试了什么:



我使用以下脚本



CONVERT(NVARCHAR(15),CONVERT(DATE,'06 / 15/2017',101 ),23)



但对于一些随机记录我的数据保存而不转换(即记录保存为'06 / 15/2017'而不是'2017-06-15 ')。



如果有人有替代解决方案,请帮助我。



非常感谢你提前。

解决方案

引用:

我的目的是仅保存日期在yyyy-mm-dd日期格式中为NVARCHAR(100)数据类型字段。



简单:不要。

永远不要将日期存储为DATE以外的任何其他日期或DATETIME - 它总是会造成大量问题。一旦你在那里得到了不好的数据 - 你会,并且它看起来像你有 - 你不能轻易说出预期的价值是什么,这使得它不可靠。例如,如果记录保存为06/05/2017,则2017年5月6日或2017年6月5日?您无法分辨,两种形式都完全有效,具体取决于用户输入它们。更糟糕的是2004年5月6日的06/05/04,2004年6月5日,2006年5月4日,还是别的什么?一旦该数据到达数据库,您无法分辨,因为有关原始用户输入区域设置的所有信息(确定如何将日期格式化为该特定PC上的字符串)不再可用。



始终将数据存储为适用的数据类型:INT中的整数,FLOAT(或DECIMAL)中的浮点数以及DATE或DATETIME中的日期 - 并将输入和输出值的格式保留给演示文稿软件,而不是后端。 / BLOCKQUOTE>

Hi,

My aim is to save the date only in yyyy-mm-dd date format into NVARCHAR(100) datatype field.

I have a SQL script to convert all the date format into "yyyy-mm-dd" and I have the NVARCHAR(100) field to store the date. But some random record saved as in the same date format without converting.

What I have tried:

I am using the following script

CONVERT(NVARCHAR(15),CONVERT(DATE,'06/15/2017',101),23)

But for some random record my data save without converting (i.e record save as '06/15/2017' instead of '2017-06-15').

If anyone have alternate solution for this kindly help me.

Thank you so much in advance.

解决方案

Quote:

My aim is to save the date only in yyyy-mm-dd date format into NVARCHAR(100) datatype field.


Simple: don't.
Never store dates as anything other than DATE or DATETIME - it always causes massive problems down the line. Once you get bad data in there - and you will, and it looks like you have - you cannot easily tell what the intended value was and that makes it unreliable. For example, if the record is saved as "06/05/2017" is that 6th May 2017, or 5th Jun 2017? You can't tell, and both forms are perfectly valid depending on the user inputting them. Worse, is "06/05/04" 6th May 2004, 5th Jun 2004, 4th May 2006, or "something else"? Once that data hits the DB, you can't tell because all information about the original user input locale (which determines how dates are formatted into strings on that particular PC) is no longer available.

Always store data as the applicable datatype: integers in INT, floats in FLOAT (or DECIMAL) and dates in DATE or DATETIME - and leave the formatting of input and output values to the presentation software, not backend.


这篇关于日期未转换为yyyy-mm-dd随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆