?
Solved

SQL Stored Porcedure/Output Text File

Posted on 2012-03-12
5
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

801 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