You must Sign In to post a response.
  • Category: SQL Server

    Getting date range data

    I have a column TransationDate is decimal data type the data's like 20170121 I want to display data's in date range, The database columns data like 20170121 ..............20170129. I want to display data's these date range based on given strartdate,enddate as criteria. In my query it is displayed only one record. what's wrong my query could any body advise to me.

    declare @StartDate varchar(10)
    declare @EndDate varchar(10)
    set @StartDate ='20170121'
    set @EndDate ='20170129'

    SELECT *
    FROM LCOM.dbo.PORCPAH PORCPAH
    WHERE CONVERT(DATETIME, CONVERT(VARCHAR(8), PORCPAH.TRANSDATE), 112) >= @StartDate
    AND CONVERT(DATETIME, CONVERT(VARCHAR(8), PORCPAH.TRANSDATE), 112) <= @EndDate
  • #769602
    Can you get us the table definition and insert script for the data in that table. We can figure out why it is returning only one row as result.
    Please mark this as Answer, if this helps

    Regards,
    Alwyn Duraisingh.M 
    << Database Administrator >>
    Jesus saves! The rest of us better make backups...

  • #769620
    Brite,

    You need to parse your @start and @endDate values to Datetime as well, the same way you are casting it in left hand side of condition..

    Thanks!

    Thanks!
    Anjali Bansal

    ~Give your best and lead the world

  • #769649
    I Hope, table has created with varchar for the date columns. so, try to check with below query.

    SELECT *
    FROM LCOM.dbo.PORCPAH PORCPAH
    WHERE CONVERT(VARCHAR(8), PORCPAH.TRANSDATE), 112) >= CONVERT(VarChar(8), @StartDate, 112)
    AND CONVERT(VARCHAR(8), PORCPAH.TRANSDATE), 112) <= CONVERT(VarChar(8), @StartDate, 112)

    Regards,
    Ramana Reddy

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments