{"id":1419,"date":"2017-08-12T12:15:25","date_gmt":"2017-08-12T12:15:25","guid":{"rendered":"http:\/\/www.theSQLReport.com\/?p=1419"},"modified":"2017-08-12T12:19:24","modified_gmt":"2017-08-12T12:19:24","slug":"datetime-to-the-millisecond-in-a-filename","status":"publish","type":"post","link":"https:\/\/www.theSQLReport.com\/?p=1419","title":{"rendered":"DateTime to the Millisecond in a Filename"},"content":{"rendered":"<p>My goal was create a filename with the DateTime format to the millisecond to be used in a stored procedure.<\/p>\n<p>First was to get the DateTime format I wanted without all the characters ( :, -, .) .<\/p>\n<pre>select convert(varchar(30), getdate(), 126) as DateTime_To_Milliseconds;\r\n\r\nselect left(replace(replace(replace(replace(convert(varchar(35), getdate(), 126),'-',''),':',''),' ',''),'.',''),18) \r\nas DateTime_To_Milliseconds_Without_Addition_Characters;<\/pre>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1420 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds.jpg\" alt=\"TimeDateToMilliseconds\" width=\"1313\" height=\"439\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds.jpg 1313w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds-300x100.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds-768x257.jpg 768w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds-1024x342.jpg 1024w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMilliseconds-210x70.jpg 210w\" sizes=\"(max-width: 1313px) 100vw, 1313px\" \/><\/a><\/p>\n<p>Below is SQL that can be tested in a stored procedure:<\/p>\n<pre>set nocount on;\r\n\r\ndeclare @fileDate varchar(30);\r\n\r\ndeclare @fileName varchar(30);\r\n\r\nset @fileDate = left(replace(replace(replace(replace(convert(varchar(35), getdate(), 126),'-',''),':',''),' ',''),'.',''),18);\r\n\r\nset @fileName = 'TPA_' + @fileDate +\u00a0 '.RPT';\r\n\r\nselect @fileName as File_Name_With_Date_To_Milliseconds;\r\n\r\n-- Again display for testing in my stored procedure \r\nprint @fileName;<\/pre>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMillisecondsForStoredProcedure.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1421 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2017\/08\/TimeDateToMillisecondsForStoredProcedure.jpg\" alt=\"TimeDateToMillisecondsForStoredProcedure\" width=\"1173\" height=\"443\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a class=\"readmore\" href=\"https:\/\/www.theSQLReport.com\/?p=1419\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1421,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[101,102,103],"class_list":["post-1419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server","tag-filename","tag-getdate","tag-millisecond"],"_links":{"self":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/1419"}],"collection":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1419"}],"version-history":[{"count":4,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/1419\/revisions"}],"predecessor-version":[{"id":1425,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/1419\/revisions\/1425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/media\/1421"}],"wp:attachment":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}