日期:2014-05-19  浏览次数:20916 次

如何更改sql server 2005 的默认时间格式
sql中默认的格式是月日年,而vs中得到的日期格式为年月日,
结果我用
  System.DateTime.Now.ToString(   )
更新到sql中的日期型数据,   日期就不对了.
请问,sql中该如何修改,才能更改它的默认时间格式.


------解决方案--------------------
1 取值后格式化
{0:d}小型:如2005-5-6
{0:D}大型:如2005年5月6日
{0:f}完整型

2 当前时间获取
DateTime.Now.ToShortDateString

3 取值中格式化
SQL Server里面可能经常会用到的日期格式转换方法:

举例如下:

select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ), '- ', ' '), ' ', ' '), ': ', ' ')
20040912110608

select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12

select CONVERT(varchar(12) , getdate(), 112 )
20040912

select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12

其它我不常用的日期格式转换方法:

select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004

select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004

select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004

select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004

select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004

select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004

select CONVERT(varchar(12) , getdate(), 108 )
11:06:08

select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1

select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004

select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1

select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177

//string处理
string.ToString( "0.00 ")
String.Format( "{0:N2} ",string)

//绑定处理
<asp:BoundColumn DataField= "TotalPrice " HeaderText= "小计(元) " DataFormatString= "{0:c} "> </asp:BoundColumn>

<ItemTemplate> <# DataBinder.Eval(Container.DataItem, "Order_TotalPri ", "{0:c} ") %> </ItemTemplate>

//对价格进行四舍五入
string myPrice = String.Format( "{0:N2} ", Math.Round((Convert.ToDouble(price)*Convert.ToDouble(discount)*0.1),2));

DATAGRID
{0:0.0}