如何将日期时间值转换为日期 [英] How to convert datetime value to date
本文介绍了如何将日期时间值转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据库中有一个datetime值.我需要将日期作为查询字符串传递,所以我想通过使用convert函数将datetime转换为date.
这是我的代码
I have a datetime value in database.i need to pass date as querystring so i want to convert the datetime into date by using convert function.
here is my code
SqlCommand comm1 = new SqlCommand("select distinct trackorder,netprice,recip_name,bill_name,convert(varchar,date,103) from tbl_trackorder join tbl_users_products on tbl_trackorder.usersess=tbl_users_products.usersession join tbl_recipient on tbl_users_products.usersession=tbl_recipient.usersession join tbl_billing on tbl_recipient.usersession=tbl_billing.usersession join tbl_session_user on tbl_session_user.usersession=tbl_recipient.usersession and tbl_session_user.usersession=tbl_billing.usersession where tbl_session_user.date='"+date+"'", sqlcon);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = comm1;
DataSet dss = new DataSet();
dap.Fill(dss);
return dss;
它将返回错误:The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
谁能帮我.
谢谢
it will return a error:The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
can any one help me.
Thanks
推荐答案
OT:
我有一些建议给你
创建表时,不要在名称前加上tbl_前缀-只会使它们更难阅读.
将sql转换为存储过程-使停止sql注入攻击更有效,更容易-但请确保您不给它们加上sp前缀-仅系统存储过程应以此开头(如果这样做会降低性能) -您还可以使用查询分析器来帮助您优化它们
确保为所有联接等正确设置索引-如果所有sql都在存储过程中,这会更容易,因为您可以使用索引调整向导来帮助您
我敢肯定还有更多,但是我的时间受到了限制
OT:
I have a few recommendations for you
When you create tables don''t prefix the names with tbl_ - just makes them harder to read.
Convert for sql to a stored procedure - makes it much more efficient and easier to stop sql injection attacks - but make sure you do NOT prefix them with sp - only system stored procedures should start with this (there is a performance hit if you do) - you can also use query analyser to help you optimise them
Make sure you have your indexes set up correctly for any joins etc - this is easier if you all your sql is in stored procedures as you can use the index tuning wizard to help you
I am sure that there are more, but I am a bit limited in time
尝试使用dd-MMM-yyyy
格式的日期.
where tbl_session_user.date=''"+ date.ToString(dd-MMM-yyyy) +"''"
Try using date indd-MMM-yyyy
format.
where tbl_session_user.date=''"+ date.ToString(dd-MMM-yyyy) +"''"
这篇关于如何将日期时间值转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文