DateTime to the Millisecond in a Filename

My goal was create a filename with the DateTime format to the millisecond to be used in a stored procedure.

First was to get the DateTime format I wanted without all the characters ( :, -, .) .

select convert(varchar(30), getdate(), 126) as DateTime_To_Milliseconds;

select left(replace(replace(replace(replace(convert(varchar(35), getdate(), 126),'-',''),':',''),' ',''),'.',''),18) 
as DateTime_To_Milliseconds_Without_Addition_Characters;

TimeDateToMilliseconds

Below is SQL that can be tested in a stored procedure:

set nocount on;

declare @fileDate varchar(30);

declare @fileName varchar(30);

set @fileDate = left(replace(replace(replace(replace(convert(varchar(35), getdate(), 126),'-',''),':',''),' ',''),'.',''),18);

set @fileName = 'TPA_' + @fileDate +  '.RPT';

select @fileName as File_Name_With_Date_To_Milliseconds;

-- Again display for testing in my stored procedure 
print @fileName;

TimeDateToMillisecondsForStoredProcedure

 

 

Leave a Comment