Solved

SQL Stored Porcedure/Output Text File

Posted on 2012-03-12
5
322 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now