Thursday, 30 January 2014 12:04

Convert a date to varchar

Share this post

This function return a varchar with the date and time from a datetime parameter.

The string returned is in french but can be adapted in other language.

CREATE FUNCTION [dbo].[ConvertDateToVarchar]
(	
	@Date DateTime
)
RETURNS TABLE 
AS
RETURN 
(
	SELECT	DateStr =	CASE	WHEN @Date Is Null THEN 'Jamais' 
								WHEN (DatePart(hour, @Date) = 0 AND DatePart(Minute, @date) = 0) THEN CONVERT(VARCHAR, @Date, 105)
						ELSE	CONVERT(VARCHAR, @Date, 105) + ' à ' + 
								REPLACE(CONVERT(VARCHAR(5), @Date, 108),':', 'h')
						END
)

Read 23323 times Last modified on Thursday, 30 January 2014 12:04
More in this category: Search in system object »