Sql server如何根据设置获取当前日期

时间:2013-05-15 10:44:59

标签: sql-server

我们知道人们在他们的电脑中设置他们的日期格式,如dd / MM / yyyy,或者可能是MM / dd / yyyy。所以我只想根据系统设置显示日期。

现在我正在这样做

CONVERT(VARCHAR(10),GETDATE(),101) AS DateOnly;

如果可能请帮助

2 个答案:

答案 0 :(得分:3)

这在SQL Server中无法完成。它不知道客户端的设置是什么。

您可以在调用应用程序或Web服务器中执行此操作。

答案 1 :(得分:3)

我认为不能按照你想要的方式完成......

如果用户使用查询工具进行查询,则该查询工具中有关于如何显示日期的设置。

如果您有应用程序层(Web,客户端等),则需要根据某处的区域设置对其进行格式化。

这可以是

a)在SQL-Server上的SQL中,如下所示:

DECLARE @d DATETIME = '01/01/2011';
SELECT FORMAT ( @d, 'd', 'en-US' ) AS US_Result;
SELECT FORMAT ( @d, 'd', 'fr-FR' ) AS FR_Result;
SELECT FORMAT ( @d, 'd', 'de-DE' ) AS DE_Result;

(应用程序将语言环境从客户端传递到SQL)

b)在应用程序代码.net / java中使用例如格式类等

public class FormatDate
{
   public static void Main()
   {
      DateTime dt = DateTime.Now;
      // Sets the CurrentCulture property to U.S. English.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      // Displays dt, formatted using the ShortDatePattern
      // and the CurrentThread.CurrentCulture.
      Console.WriteLine(dt.ToString("d"));

      // Creates a CultureInfo for German in Germany.
      CultureInfo ci = new CultureInfo("de-DE");
      // Displays dt, formatted using the ShortDatePattern
      // and the CultureInfo.
      Console.WriteLine(dt.ToString("d", ci));
   }
}

c)在客户端,例如如果用户正在查看您需要使用的网页,例如此处概述的javascript:

Display date/time in user's locale format and time offset