Dec 01

Format SQL Date Function (formatSqlDate)

Formats a date/date string for use in a SQL statement.

function formatSqlDate(inDate,inTime)
	if inDate <> "" or inTime <> "" then
		if isdate(inDate) and isDate(inTime) then
			FormatSqlDate = year(inDate) & "-" &_
							twodigit(month(inDate)) & "-" &_
							twodigit(day(inDate)) & " " &_
							twoDigit(hour(inTime)) & ":" &_
							twoDigit(minute(inTime)) & ":" &_
							twoDigit(second(inTime))
		elseif isDate(inDate) and not isDate(inTime) then
			FormatSqlDate = year(inDate) & "-" &_
							twodigit(month(inDate)) & "-" &_
							twodigit(day(inDate)) & " " &_
							"00:00:00"
		elseif not isDate(inDate) and not isDate(intime) then
			FormatSqlDate = "1900-01-01 00:00:00"
		else
			FormatSqlDate = "1900-01-01 " &_
							twoDigit(hour(inTime)) & ":" &_
							twoDigit(minute(inTime)) & ":" &_
							twoDigit(second(inTime))
		end if
	end if
end function

Permanent link to this article: https://www.wperry.net/code/formatsqldate/

Leave a Reply

Your email address will not be published.