DateTime is used in C. toString (yyyy-MM-dd), DateTime. toString (MMddyyyy), a date-to-String Conversion method, is useless in SQLserver, and thus a FUNCTION is written. dateTime in. net. the ToString (formatprovide) method is similar, but only the date part is implemented.
DateTime is used in C. toString ("yyyy-MM-dd"), DateTime. toString ("MM/dd/yyyy"), a date-to-String Conversion method, is useless in SQL server. Therefore, a FUNCTION is written with the FUNCTION. dateTime in. net. the ToString ("formatprovide") method is similar, but only the date part is implemented.
DateTime is used in C. toString ("yyyy-MM-dd"), DateTime. toString ("MM/dd/yyyy"), a date-to-String Conversion method, is useless in SQL server. Therefore, a FUNCTION is written with the FUNCTION. dateTime in. net. the ToString ("formatprovide") method is similar, but only the date part is implemented. If you are interested, you can add the time part.
Create function fn_DateToString (@ date datetime, @ format varchar (20 ))
Returns varchar (20)
As
Begin
Declare @ result varchar (20)
Select @ result = (replace (@ format, 'yyyy', '20' + substring (convert (char (8), @ date, 3 ), ), 'mm', substring (convert (char (8), @ date, 3),), 'dd', substring (convert (char (8 ), @ date, 3), 1, 2 )))
Return @ result
End
Usage:
Select dbo. fn_datetostring (getdate (), 'yyyy-MM-dd ')
Expected result:
Author: