在SQL Server 2008中转换日期格式 [英] Converting a date format in SQL server 2008

查看:124
本文介绍了在SQL Server 2008中转换日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个数据库,其日期结构在YYYY-MMM-DD(我猜这是默认的)但是当我在程序中调用日期转换为DD-MM-YYYY,我觉得数据库中的内容应该是相同的。所以请任何人帮助我



SHOUTING删除 - OriginalGriff [/ edit]

解决方案

< blockquote> select convert(varchar,getdate(),106)


实际上并非如此,虽然它可能看起来像。

SQL(和几乎所有其他系统都没有以任何特定格式存储日期 - 它从任意时间点开始存储为毫秒数。



只有在DateTime项目将转换为字符串以供显示,它获取任何格式,并由执行转换的系统指定。如果您在进行转换时没有明确指定格式,默认情况下将使用该计算机的本地化设置。



我无法明确告诉您什么改变以获得你想要的输出,因为我不知道你在转换的哪个阶段,可能很容易有三种不同的计算机可以参与:SQL服务器PC,你的应用程序运行的服务器,以及任何客户端PC进行实际演示。


C#示例:



theDate.ToString(yyyy-MM-dd )



SQL;

select convert(varchar,getDate(),120)


Hello all,

I have a db in which the date structure is in YYYY-MMM-DD(which is default in nature i guess) but when i call in the programe the date is converting to DD-MM-YYYY, i feel what is there in the db it should come as the same. so please can any one help me out

[edit]SHOUTING removed - OriginalGriff[/edit]

解决方案

select convert(varchar,getdate(),106)


That isn't actually the case, although it may look like it.
SQL (and nearly every other system) does not store a date in any particular format - it is stored as a number of milliseconds since an arbitrary point in time.

It's only when a DateTime item is converted to a string for display that it "acquires" any formatting, and that is specified by the system doing the conversion. If you do not explicitly specify a format when you do the conversion, the localisation settings for that computer will be used by default.

I can't explicitly tell you what to change to get the output you want, because I have no idea at what stage you are doing the conversion, and there could easily be three different computers that could be involved: the SQL server PC, the server your app is running on, and any client PC doing the actual presentation.


Example in C#:

theDate.ToString("yyyy-MM-dd")

SQL;
select convert(varchar,getDate(),120)


这篇关于在SQL Server 2008中转换日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆