Today I bring to you a useful SQL function to get a string like YYYYMMDDHHMMSS (example for 30-10-2009 20:01:01 will be 20091030200101).
Use YourDatabase
Create function [dbo].[F_Datetime]()
returns varchar(50)
as
begin
declare @datanext varchar(50)
select @datanext =
(select (substring ((select convert(varchar(10),getdate(),103)),7,4)))+ -- Year
(select (substring ((select convert(varchar(10),getdate(),103)),4,2)))+ -- Month
((select (substring ((select convert(varchar(10),getdate(),103)),1,2)))+ -- Day
replace((select convert(varchar(8),getdate(),108) ),':','')) -- hour and minutes HHMMSS
return @datanext
end
Then you can use like these:
Use YourDatabase
Go
Select [dbo].[F_Datetime]()
Then your result looks like these:
20091030203830
SQL Function to Concatenate DateTime
Publish by
Jorge Cunha
on
20:41
Friday, October 30, 2009
Etiquetas:
SQL
,
SQL Expressions
,
SQL Tips and Tricks