我按照日期时间格式做了一个日期.但是,我将其作为字符串/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 的日期或日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
修改现有小数位信息Modify Existing decimal places info(修改现有小数位信息)
多次指定相关名称“CONVERT"The correlation name #39;CONVERT#39; is specified multiple times(多次指定相关名称“CONVERT)
T-SQL 左连接不返回空列T-SQL left join not returning null columns(T-SQL 左连接不返回空列)
从逗号或管道运算符字符串中删除重复项remove duplicates from comma or pipeline operator string(从逗号或管道运算符字符串中删除重复项)
将迭代查询更改为基于关系集的查询Change an iterative query to a relational set-based query(将迭代查询更改为基于关系集的查询)
将零连接到 sql server 选择值仍然显示 4 位而不是concatenate a zero onto sql server select value shows 4 digits still and not 5(将零连接到 sql server 选择值仍然显示 4 位而不是 5)