I have the following function below.
It returns a date like Feb 29 2012 10:00PM. Is there a way to make it return the format as 2/29/2012 10:00PM
CREATE FUNCTION scrubDateString
(
-- Add the parameters for the function here
@inputDate varchar(150)
)
RETURNS DATETIME
AS
BEGIN
-- Declare the return variable here
DECLARE @Result DATETIME
-- Add the T-SQL statements to compute the return value here
DECLARE @tmpDate datetime
SET @Result = null
IF (ISDATE(@inputDate)=1)
BEGIN
SET @Result = DATEADD(HH, 5, @inputDate)
END
-- Return the result of the function
RETURN @Result
END
The function itself is returning a Date and time, “Feb 29 2012 10:00PM” and “2/29/2012 10:00AM” are merely string representations of that date (I am assuming that the AM and PM switch are not relevant to the question and are just a mistake). IF you want to format the date as a particular string look at the CONVERT function. e.g:
Although I’d strongly advise leaving your date as a date, and doing any formatting on the application side if at all possible. If I have assumed wrong about the AM PM switch you could alter the code above to the following: