[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Stored Porcedure/Output Text File

Posted on 2012-03-12
5
Medium Priority
?
340 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 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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

830 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