SQL Server - Compare dates or datetime stored as a string or nvarchar?(SQL Server - 比较存储为字符串或 nvarchar 的日期或日期时间?)
问题描述
我按照日期时间格式做了一个日期.但是,我将其作为字符串/nvarchar 而不是日期时间存储在我的数据库中.我能比较这些日期吗?这是一种不好的做法吗?
I made a date according to the datetime format. But, I stored this in my DB as a string/nvarchar and not a datetime. Will I be able to compare such dates ? Is this a bad practice ?
我现在使用 nvarchar 来存储日期时间.
I am using nvarchar to store a datetime as of now.
推荐答案
是的,您仍然可以比较它们,但这肯定是一个不好的做法,因为您需要将此字符串转换为日期数据类型才能进行比较它们之间.如果您在列上定义了索引,则它们将不再使用,因为该列将被转换,并且会导致大型数据库的性能下降.
Yes you can still be able to compare them but this is certainly a bad practice because you will need to convert this strings into dates data type to be able to compare between them. If you have indexes define on the column, they will not be used anymore since the column will be converted and it will cuase slow performance on large database.
比较日期的例子是这样的:
An example on comparing dates is like this:
SELECT *
FROM tableName
WHERE CONVERT(DATETIME, dateSTRColumn, XXX) > GETDATE()
其中 XXX
是存储为字符串的日期的当前格式.
where XXX
is the current format of the date stored as string.
这篇关于SQL Server - 比较存储为字符串或 nvarchar 的日期或日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server - 比较存储为字符串或 nvarchar 的日期或日


- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01