medusa
medusa

Reputation: 541

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 varchar but to datetime. Can anyone give me an idea please?

Upvotes: 2

Views: 208

Answers (1)

marc_s
marc_s

Reputation: 754388

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.

Upvotes: 2

Related Questions