Updated date time function and sql query