Solved

SQL Stored Porcedure/Output Text File

Posted on 2012-03-12
5
331 Views
Last Modified: 2012-03-15
I am trying to create a SQL Stored procedure that will create a report on a nightly bases.  
The requirement for the file name is mrpt.mmddyyyyhhmm.txt  
I need to have my stored procedure name the file this way so that no one has to touch it manually.  
The best I have been able to get is it naming the file mrpt.mmd (the mmd sets as the extension of the file and it cannot be opened).  
Thank you in advance for any help with this issue.
0
Comment
Question by:smc1234
  • 2
  • 2
5 Comments
 

Expert Comment

by:vinaypugalia
ID: 37709480
I would like to have some more clarification on your query.
1. Is the idea just to create a txt file on the local system or to even mail it?
2. I did not get your naming convention for the file - mrpt.mmddyyyyhhmm.txt What does "mrpt" here. Is this a fixed hardcoded value of something dynamic?
0
 
LVL 3

Expert Comment

by:tsnirone
ID: 37709510
0
 
LVL 3

Accepted Solution

by:
tsnirone earned 500 total points
ID: 37709529
So for the filename it should be something like:

declare @TheDate datetime

set @TheDate = GetDate()

select

'mrpt.'

+ right('0' + convert(varchar,datepart(Month,@TheDate)), 2) --The Month

+ right('0' + convert(varchar,datepart(Day,@TheDate)), 2) --The Day

+ convert(varchar,datepart(Year,@TheDate)) --For the Year

+ right('0' + convert(varchar,datepart(hour,@TheDate)), 2) --The hour

+ right('0' + convert(varchar,datepart(minute,@TheDate)), 2) --The minute

+ '.txt'
0
 

Expert Comment

by:vinaypugalia
ID: 37710007
If you want something very static, please try this tsql -
DECLARE @DateSeparator AS CHAR(1) = '/'
SELECT 'mrpt.'+ 
       LEFT(REPLACE(REPLACE(CONVERT(VARCHAR,GETDATE(),101) + CONVERT(VARCHAR,GETDATE(),8),@DateSeparator,''),':',''),12) +
       '.txt'

Open in new window

0
 

Author Closing Comment

by:smc1234
ID: 37724721
Thanks a lot :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question