sql - Storing the previous month as a char with a leading 0 for single digit months -


I have a field filled with dates, but they are stored in varchars as they are in the following format:

  • 01302013
  • 09232007

    I am trying to write a query that will only allow the following three parameters The last four characters of Year

    • The previous ward "date" match for last month
    • The previous varchar "date" The first two letters of last march Match up with
    • The value in a completely different date field (which is actually stored as a date) comes within the last month

      My efforts lasted me The month and the previous month have stopped trying to store in two variables, and then on varchar "date" a LEFT () and right () "to see if they match those two variables." / P>

      The problem is that MONTH is being stored as a digit, and I It needs to be stored in the form of "0" + month digits (only in the case of single digit months), except that it is not working just for the year, so it is completely closed. Hope someone can help you!

      Even what I have so far:

        DECLARE @MONTHAgo date expiration @MonthString char DECLARE @YearString char SET @monthAgo = DATEADD (month, -1, GETDATE ()) SET @MonthString = MONTH (@MonthAgo) - SET @YearString = YEAR (@MonthAgo) is not working - Not working Except (*) jwemaildb.LogFileRecords WHERE date & gt; = DATEADD (month, DATEDIFF (month, 0, gaterate ()) - 1, 0) and date & lt; DATEADD (month, DATEDIFF (month, 0, GETDATE ()), 0) and right (eAlertSentDate, 4) = @YearString and left (eAlertSentDate, 2) = @MonthString    < P> 

      In fact, you need to change the "date" to "date" in the ISO standard

        SELECT COUNT (* ) Logfilereaks from jwemaildb where date & gt; = DATEADD (month, DATEDIFF (month, 0, gaterate ()) - 1, 0) and date & lt; DATEADD (1, DAY, (GETDATE ()), DATEADD (date, DATEDIFF (month, 0, GETDATE ()), 0) and CAST (right (eAlertSentDate, 4) + LEFT (eAlertSentDate, 4) AS date) DATEADD Mm / -1, GETDATE ()) and DATEADD (DD, 1 - DAY (GETDATE ()), GETDATE ())   

      Simple demo < / Html>

  • Comments

    Popular posts from this blog

    excel vba - How to delete Solver(SOLVER.XLAM) code -

    github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

    ios - Replace text in UITextView run slowly -