开发者

sql datetime formats conversion

How can i convert a datetime of type dd-mm-yyyy to dd/mm/yyyy in sql? I dont want to convert to varcha开发者_C百科r but to datetime. Can anyone give me an idea please?


A DateTime is a DateTime is a DateTime - it doesn't have " a format" - it's just a DateTime.

The format or display options only come into play when you want to display the DateTime as a string.

of type dd-mm-yyyy to dd/mm/yyyy

To me, this sounds as if:

  • you have string representing a date, in the format dd-mm-yyyy
  • you want to convert that to a DateTime and then again display a string representing that DateTime in the dd/mm/yyyy format

Right??

Check out the MSDN SQL Server Books Online for CAST and CONVERT - it lists all possible, valid, supported string formats representing a date and time.

Format dd-mm-yyyy is style no. 105, so this will convert your first string:

SET @YourDateTime = CONVERT(DATETIME, '23-11-2010', 105)

Now you have your DATETIME value (Nov 23, 2010) in @YourDateTime - just a DateTime, no formatting applied.

If you want to display that date value in format dd/mm/yyyy, which is style no. 103, use this snippet of T-SQL to achieve this:

SELECT CONVERT(VARCHAR(50), @YourDateTime, 103)

This will display 23/11/2010.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜