简体   繁体   English

SQL 服务器:将列从 nvarchar 更改为 int

[英]SQL server: alter column from nvarchar to int

This table has filedate(nvarchar) and Filename.该表具有文件日期(nvarchar)和文件名。 The format needs to be in MMDDYYYY for the filedate.文件日期的格式需要为 MMDDYYYY。

在此处输入图像描述 I used我用了

 replace(cast(filedate as varchar),'-','') 

and changed the datatype to int, which worked, but not in the format that I need.并将数据类型更改为 int,这有效,但不是我需要的格式。 在此处输入图像描述

Do I need to update the Fname and then try this or is there another way to do this?我是否需要更新 Fname 然后尝试此操作,或者是否有其他方法可以执行此操作?

Thank you in advance.先感谢您。

Any column that is expected to have leading zeros is not a number.任何预期有前导零的列都不是数字。 So, you want a string.所以,你想要一个字符串。 My recommendation is to convert the value to a date and then format it in the way you want:我的建议是将值转换为日期,然后以您想要的方式对其进行格式化:

select format(convert(date, '4-17-2020', 110), 'MMddyyyy')

Or, better yet, simply convert the value to date.或者,更好的是,只需将值转换为日期。 It is a date.这是一个约会。 So you should store and retrieve it as a date.因此,您应该将其存储和检索为日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM