問題描述
我按照日期時間格式做了一個日期.但是,我將其作為字符串/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模板網!