timestamp question

Posted on 2007-10-10
Last Modified: 2013-11-30

I have a Activex Script in DTS package. It should save in file in mmddyy format.
when it is 10-09-2007 it should save as 100907 but instead it is saving as 10907. what should i change in my code. Below is the code.

Thanks in advance,

Option Explicit

Function Main()

      Dim oRS
      Set oRS = DTSGlobalVariables("RSTables").Value

    Const ForReading = 1
    Const ForWriting = 2
dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim ResultFile, objResult
             dim strSourceFile
             dim strFilePath
             strFilePath = DTSGlobalVariables("FilePath").Value            
      ResultFile = strFilePath   & Month(Date) & day(date-1) & right(year(date),2) & DTSGlobalVariables("SourceFileName").Value    
Set objResult = fso.OpenTextFile(ResultFile, ForWriting, True)
      do while not oRS.EOF             
             objResult.WriteLine       oRS.Fields(0).Value            


        Set objResult = Nothing

    Set fso = Nothing

      Set oRS = Nothing

      Main = DTSTaskExecResult_Success
End Function
Question by:NK9
    LVL 11

    Expert Comment

    A method of printing numbers with leading zeros is to add a factor of 10 more than the number of zeros.  For instance, if you wanted up to 3 zeros it would be something like

    right (cstr (1000 + n), 3)

    In your case, you wanted 2 leading zeros, so add 100 and take the rightmost 2 digits.

    Try this

    ResultFile = strFilePath   & _
        right(cstr(100+Month(Date)), 2) & _
        right(cstr(100+day(date-1)), 2) & _
        right(year(date),2) & DTSGlobalVariables("SourceFileName").Value

    Also test on single digit months and days.  You will probably find that the original source on 6 May would read 5607 instead of 050607.
    LVL 65

    Accepted Solution

    Hi, change this line:
    ResultFile = strFilePath & Month(Date) & day(date-1) & right(year(date),2) & DTSGlobalVariables("SourceFileName").Value

    to this:
    ResultFile = strFilePath & Right("00" & Month(Date), 2) & Right("00" & Day(Date-1), 2) & Right(year(date),2) & DTSGlobalVariables("SourceFileName").Value

    What I have done there is add two "Right" functions to the Month and Day functions.  This gets the two right most characters from a string like:
    0010 for October, being 10, and
    009 for the ninth day, being 09



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    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.

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now