13

I am returning either a DATETIME or the NVARCHAR = 'MULTIPLE' depending on whether or not an action has been performed more than one time.

So I am trying to store the DATETIME in its normal format '2012-10-23 13:59:47.000' but as an NVARCHAR. SQL wants to make it 'Oct 23 2012 12:40PM' How can I do this?

Right now I am doing:

CAST(r.Date_And_Time) AS NVARCHAR(30))
1
  • 1
    CONVERT, format number 121.
    – GSerg
    Commented Dec 7, 2012 at 23:13

2 Answers 2

14
Declare @CreatedDate datetime
Select @CreatedDate='20121210'
Select CONVERT(VARCHAR,@createdDate, 21)
1
  • I noticed converting datetime to nvarchar converting the format to 12 format by default , but this solutions solved this issue , thank you
    – Omar Isaid
    Commented Feb 22, 2020 at 13:09
9

Use CONVERT. It has format parameter.

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

CONVERT(NVARCHAR(23), r.Date_And_Time, 121)

https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

1
  • select CONVERT(nvarchar(23),r.Date_And_Time,121)
    – JohnLBevan
    Commented Dec 7, 2012 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.