to_datetime.sql 524 B

123456789101112131415161718192021222324252627282930
  1. SET QUOTED_IDENTIFIER ON
  2. GO
  3. SET ANSI_NULLS ON
  4. GO
  5. CREATE FUNCTION [dbo].[to_datetime] (@d varchar(255))
  6. RETURNS datetime
  7. with execute as CALLER
  8. AS
  9. BEGIN
  10. declare @to_datetime datetime;
  11. if @d is null
  12. return null
  13. if (charindex(' ', @d) = 0)
  14. set @d = @d + 'T00:00:00';
  15. else
  16. set @d = replace(@d, ' ', 'T') + ':00';
  17. if isdate(@d) = 0
  18. return null
  19. set @to_datetime = convert(datetime, @d)
  20. return (@to_datetime);
  21. END
  22. GO
  23. SET QUOTED_IDENTIFIER OFF
  24. GO
  25. SET ANSI_NULLS OFF
  26. GO
  27. GO