convert an INT field into a datetime field

  • After reading, I wasn't 100% sure if you wanted to go from int to date, or date to int. I've included a way to do both. Hopefully this will help.

    --Int to Date

    DECLARE @Date INT

    SET @Date = 201506

    SELECT

    [AsInt] = @Date

    ,[AsDate] = CONVERT(DATETIME,CONVERT(VARCHAR,@Date)+'01')

    --Date to Int

    DECLARE @Date DATETIME

    SET @Date = '2015-06-01'

    SELECT

    [AsDate] = @Date

    ,[AsInt] = LEFT(CONVERT(varchar,@Date,112),6)

  • GOOD DAY SqlSanctum,

    Maybe I didn't explain myself correctly, or I'm just confused.

    Both fields

    @BeginPeriod INT,

    @EndPeriod INT

    are being used as parameter(s) in my where clause

    WHERE TT11.PERIOD >= ''+@BeginPeriod+''

    AND TT11.PERIOD <= ''+@EndPeriod+''

    am I replacing the where clause with the script you supplied ? or is this to be added and if added where??

    Sorry in advance but this is new to me.

    Regards,

    David

  • Try this:

    ...

    WHERE TT11.Period>=@BeginPeriod

    AND TT11.Period<=@EndPeriod

  • dmarz96 (6/25/2015)


    GOOD DAY SqlSanctum,

    Maybe I didn't explain myself correctly, or I'm just confused.

    Both fields

    @BeginPeriod INT,

    @EndPeriod INT

    are being used as parameter(s) in my where clause

    WHERE TT11.PERIOD >= ''+@BeginPeriod+''

    AND TT11.PERIOD <= ''+@EndPeriod+''

    am I replacing the where clause with the script you supplied ? or is this to be added and if added where??

    Sorry in advance but this is new to me.

    Regards,

    David

    If the user selected date is @UserSelectedDate, for example, and is a DATE or DATETIME, and you want the year and month for that date, for both of the 2 parameters, then you could set the parameters like this.

    SET @BeginPeriod = (DATEPART(YEAR, @UserSelectedDate) * 100) + DATEPART(MONTH, @UserSelectedDate)

    SET @EndPeriod = (DATEPART(YEAR, @UserSelectedDate) * 100) + DATEPART(MONTH, @UserSelectedDate)



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Ok let me start this all over again to be clear.

    First the query works perfectly fine and bring back the correct data set. It's when dealing with the parameter that is where the confusing starts for me.

    My parameters are as followed:

    @BeginPeriod INT,

    @EndPeriod INT

    and part of my where clause

    WHERE TT11.PERIOD >= ''+@BeginPeriod+''

    AND TT11.PERIOD <= ''+@EndPeriod+''

    TT11.Period is an int field (ex 201501,201502,201502)

    my challenge is to be able to let the end user choose a date from the date picker (or calendar).

    for this example let's say the user picks 04/14/2015 from the date picker.

    I want the query to be able to know to choose 201504 period.

    After i get this corrected I want to be able to create a subscription to be able to run this report on a monthly basis and increment the 2 parameter(s) by 1 month.

    do something like this =DateAdd("M",+1,Today())

    can't do this if the parameter is not a date field.

    Hopefully I explained my issue clearer like I mention I new to this.

    Regards,

    David Martinez

  • Good Day Alvin,

    thanks for you input, but I don't understand how that is going to get me the end result that I'm looking for.

    Please be patient with me like I mentioned this is new to me.

  • You say you want to run this on a monthly basis, so if I ran this with today's date, what month should the period cover?



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • This particular report would be ran after the books are closed.

    So if it was ran today it would be for period 201505. Most cases this report would run for the previous month.

    Regards,

    David

  • dmarz96 (6/25/2015)


    This particular report would be ran after the books are closed.

    So if it was ran today it would be for period 201505. Most cases this report would run for the previous month.

    Regards,

    David

    If you always want to run for the previous month, then you could do something like:

    SET @BeginPeriod = CAST(CONVERT(VARCHAR(6), DATEADD(MONTH, -1, GETDATE()), 112) AS INT)

    SET @EndPeriod = CAST(CONVERT(VARCHAR(6), DATEADD(MONTH, -1, GETDATE()), 112) AS INT)



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • We have lots of reports that use the Period field. We just put a parameter for Period with a description that tells the user what to enter. No coding or converting needed.

    The users can do it. It's not that difficult.

    Example:

    Begin Period (yyyydd):

  • Yes this is a true statement but how do you increment the period when you schedule the report??

  • The only way that I know of to run a report with custom parameters that use coding is to use a Data Driven Subscription.

    Here is link to more information:

    https://msdn.microsoft.com/en-us/library/ms169673.aspx

    Here is the code I would use to get the rolling 12-month periods:

    select GETDATE() AS 'CURRENT_DATE'

    ,CAST(CONVERT(VARCHAR(4),YEAR(GETDATE())) + Right('0' + Convert(VarChar(2), MONTH(GetDate())), 2) AS INT) AS CURRENT_PERIOD

    ,CAST(CONVERT(VARCHAR(4),YEAR(DATEADD(mm,-12,GETDATE()))) + Right('0' + Convert(VarChar(2), MONTH(DATEADD(mm,-12,GETDATE()))), 2) AS INT) AS PERIOD_12_MONTH_PRIOR

  • If you want them to be able to just use the date picker, you should be able to just use two variables.

    The report interface supplies you with a date variable, you convert that date variable to an int, which you then use to run the code.

    In the report you'd have @ReportDate DATETIME as a parameter to the stored procedure that generates the report, then you'd convert that datetime to an integer in the code. It's a bit messy, but it's a workaround. So the report is happy that it is using a date, but your code/tables are still happy because they see an integer.

    --report UI provides the value for date, you could also provide a default value

    --of GETDATE() to @ReportDate so that the report auto runs the current year/month.

    DECLARE @CodeDate INT

    SET @CodeDate = LEFT(CONVERT(VARCHAR(6),@ReportDate,112),6) --format now YYYYMM

    That should make the date into a usable 6 digit integer for your report to recognize. Then use the variable in your where clause and everything should work normally.

  • Good Day To All, with respect and thanks to all!

    OK I think I have a work around with everyones input and a breif discussion with the users. I can get the user to enter the first of the month for both parameter just have to put verbage in the report design when interacting with the date picker.

    I include only the code that was added, prior code worked.

    --But I get this error

    Msg 241, Level 16, State 1, Procedure ALSP_ROLLING12_MONTH_REPORT_BILLABLE_HOURS_ONLY, Line 52

    Conversion failed when converting date and/or time from character string.

    -- how the stored procedure is executed

    EXEC ALSP_ROLLING12_MONTH_REPORT_BILLABLE_HOURS_ONLY '2014-05-01','2015-04-01'

    -- Declare Parameters to passed to report

    (

    @BeginPeriod DateTime,

    @EndPeriod DateTime

    )

    DECLARE @BeginPeriod2 VARCHAR(20) SET @BeginPeriod2 = CONVERT(VARCHAR(20), @BeginPeriod, 100)

    DECLARE @EndPeriod2 VARCHAR(20) SET @EndPeriod2 = CONVERT(VARCHAR(20), @EndPeriod, 100)

    -- code in my where clause

    WHERE CONVERT(DATETIME,CONVERT(VARCHAR,TT11.PERIOD)+'01') >= ''+@BeginPeriod2+''

    AND CONVERT(DATETIME,CONVERT(VARCHAR,TT11.PERIOD)+'01') <= ''+@EndPeriod2+''

  • dmarz96 (6/26/2015)


    Good Day To All, with respect and thanks to all!

    OK I think I have a work around with everyones input and a breif discussion with the users. I can get the user to enter the first of the month for both parameter just have to put verbage in the report design when interacting with the date picker.

    I include only the code that was added, prior code worked.

    --But I get this error

    Msg 241, Level 16, State 1, Procedure ALSP_ROLLING12_MONTH_REPORT_BILLABLE_HOURS_ONLY, Line 52

    Conversion failed when converting date and/or time from character string.

    -- how the stored procedure is executed

    EXEC ALSP_ROLLING12_MONTH_REPORT_BILLABLE_HOURS_ONLY '2014-05-01','2015-04-01'

    -- Declare Parameters to passed to report

    (

    @BeginPeriod DateTime,

    @EndPeriod DateTime

    )

    DECLARE @BeginPeriod2 VARCHAR(20) SET @BeginPeriod2 = CONVERT(VARCHAR(20), @BeginPeriod, 100)

    DECLARE @EndPeriod2 VARCHAR(20) SET @EndPeriod2 = CONVERT(VARCHAR(20), @EndPeriod, 100)

    -- code in my where clause

    WHERE CONVERT(DATETIME,CONVERT(VARCHAR,TT11.PERIOD)+'01') >= ''+@BeginPeriod2+''

    AND CONVERT(DATETIME,CONVERT(VARCHAR,TT11.PERIOD)+'01') <= ''+@EndPeriod2+''

    Is your period column an INT, or is there some bad data in your table? I ask because I received no error when I tried to make some sample data and execute your code. If I throw in some bad data, like making the DateInt = 2015016, then I get the same error you got.

    --Table for Sample Data

    DECLARE @Example TABLE

    (DateInt INT, Data INT)

    INSERT INTO @Example VALUES

    (201501,1),(201502,7),(201503,3),(201503,2),(201504,5),(201506,6),(201506,7),(201507,2),(201507,4),(201508,8)

    --Change the first value to 2015016 and you'll get the error message.

    --Setting the original values

    declare @beginperiod datetime , @endperiod datetime

    SET @beginperiod = '2014-05-01'

    SET @endperiod = '2015-04-01'

    --your code below

    DECLARE @BeginPeriod2 VARCHAR(20) SET @BeginPeriod2 = CONVERT(VARCHAR(20), @BeginPeriod, 100)

    DECLARE @EndPeriod2 VARCHAR(20) SET @EndPeriod2 = CONVERT(VARCHAR(20), @EndPeriod, 100)

    select * from @example

    WHERE CONVERT(DATETIME,CONVERT(VARCHAR,dateint)+'01') >= ''+@BeginPeriod2+''

    AND CONVERT(DATETIME,CONVERT(VARCHAR,dateint)+'01') <= ''+@EndPeriod2+''

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply