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))
We can convert the DATETIME value to VARCHAR value in SQL server using the CONVERT function. Convert function has three arguments. Let us convert a DATETIME value into VARCHAR with different date styles. And that's how to convert DATETIME value to VARCHAR value in SQL Server!
You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')
Use CONVERT
. It has format parameter.
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
CONVERT(NVARCHAR(23), r.Date_And_Time, 121)
https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
Declare @CreatedDate datetime
Select @CreatedDate='20121210'
Select CONVERT(VARCHAR,@createdDate, 21)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With