开发者

date format conversion

开发者 https://www.devze.com 2023-01-12 05:08 出处:网络
I have date of Birth of the format 04/08/2010(DD/MM/YYYY) and sql server store of the format 7/14/2010 12:00:00 AM (MM/DD/YYYY) and i wan to check my current da开发者_Python百科te from the stored dat

I have date of Birth of the format 04/08/2010(DD/MM/YYYY) and sql server store of the format 7/14/2010 12:00:00 AM (MM/DD/YYYY) and i wan to check my current da开发者_Python百科te from the stored database dates for the all dated greater than equal to the current date so how can i first change the formate of date and then can get appropriate result from the data baase using some query


SQL server doesn't store dates in a string format at all (assuming your column is a genuine DATETIME column or something similar)... and you shouldn't be sending your queries in string formats.

Instead use parameterized queries, where you fill the values using DateTime or DateTimeOffset - that way you don't need to worry about the string format when talking to the database. Parse the string input to your app in an appropriate format (e.g. with DateTime.TryParseExact) and then you shouldn't need to think about formatting.

If your database is genuinely using NVARCHAR or something similar to store dates, you should look to fix that.


Everything about date formats http://msdn.microsoft.com/en-us/library/aa226054(SQL.80).aspx


How are you getting the date back from SQL?

If you are using an ORM tool like NHibernate, you should be able to retrieve the data as a DateTime object which you can then compare against in your code.

If you're not using Nhibernate or LINQ (or some other implementation) and are using ado, you should be able to use DateTime.Parse() or DateTime.TryParse() on what you are retrieving to cast it to a DateTime that you can then compare in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消