1

I have a property in a table called cdate which is stored as such: 2014-10-07 05:26:17.897.

my dynamic query is something like this, CDATE is a datetime in the table and startdate and enddate are always passed and they are both datetime as well. My where clause is in a dynamic SQL query and it returns some records but for instance if CDate = 2014-10-07 05:26:17.897 and I am passing start date as 09/30/2010 08:15 pm and end date 10/07/2014 08:15 pm, i do not get my intended record back

SELECT  
CONVERT(VARCHAR(10),ServiceEntry.CDate,120) as CDate
FROM TABLE
WHERE
(Table.CDate between ''' + convert(varchar(10), @StartDate, 120)  + '''  and   ''' +  convert(varchar(10), @EndDate, 120) + ''')  

my original query

DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
   SELECT *
   FROM (
     SELECT
       TSBNumber [TSBNumber],
       SystemFullName,
       CONVERT(VARCHAR(10),ServiceEntry.ClosedDate,120) as ClosedDate
     FROM ServiceEntry 
     inner JOIN System 
       ON ServiceEntry.SystemID = System.ID
     where
      (ServiceEntry.TSBNumber IS NOT NULL)
       AND 
       (ServiceEntry.ClosedDate IS NOT NULL)
       AND
       ( 
       (''' + @SelectedTsbIDs + ''' = '''+ '0' + ''') OR
         (ServiceEntry.TSBNumber in (select * from dbo.SplitStrings_Moden(''' + @SelectedTsbIDs + ''', ''' + ',' + ''')))
        )  
        AND (
         (''' + CAST(@PlatformID AS VARCHAR(10)) + ''' = '''+ '0' + ''') 
        OR(System.PlatformID = ''' + cast(@PlatformID as varchar(10)) + ''')
        OR(''' + CAST(@PlatformID AS VARCHAR(10)) + ''' = ''' + '12' + ''' AND System.PlatformID <=  ''' + '2' + ''')
        )
        AND
   (ServiceEntry.ClosedDate between ''' + convert(varchar(10), @StartDate, 120)  + '''  and   ''' +  convert(varchar(10), @EndDate, 120) + ''')    
7
  • It is a DateTime in SQL Server 2008 table Commented Oct 7, 2014 at 19:52
  • I believe you need to convert table.cdate in the where clause the same as you have in the select. and is @startdate ALWAYS before @ENDDate? if not that's a problem and if @start or @end are NULL then you have a problem there as well... are you getting an error or just no results? Commented Oct 7, 2014 at 19:57
  • STartDate and EndDate will always be provided, the dates passed will have a timestamp in them. I get no results in the above use-case Commented Oct 7, 2014 at 20:00
  • wow I can't believe I missed this the first time... your converting a @startDate, @EndDate to varchar in the where clause... why? varchar datetime will seldom equal a true timestamp... which is why it's hit and miss... What are variables @startDate, @endDate defined as? and why convert them if they are not going to be rendered... Basically you're saying.... Take this REAL apple turn it into a picture of an orange now have the system compare the Real apple passed in to the orange you created... Commented Oct 7, 2014 at 20:04
  • @tam tam , don't cast startdate and enddate in the where caluse Commented Oct 7, 2014 at 20:05

1 Answer 1

1

Don't convert @StartDate, @EndDate to varchar as you need date comparison

SELECT  
    CONVERT(VARCHAR(10),ServiceEntry.CDate,120) as CDate
FROM 
    TABLE
WHERE
    Table.CDate BETWEEN @StartDate AND @EndDate

EDIT:

AS OP wants in dynamic query it should be written like this

@query = ' Declare @StartDate datetime = ' + value1  + ' Declare @EndDate datetime =' + value2 + ' SELECT  
    CONVERT(VARCHAR(10),ServiceEntry.CDate,120) as CDate
FROM 
    TABLE
WHERE
    Table.CDate BETWEEN @StartDate AND @EndDate '

exec(@query)
Sign up to request clarification or add additional context in comments.

7 Comments

where clause if you notice the syntax from my query it is in a dynamic query and if i dont cast it in the where clause then i get this error Conversion failed when converting date and/or time from character string.
@tamtam, if it is dynamic query. it should be like this.
i have already decalred the startdate and endate in my store procedure, please look at my post, i edited and added the original query
@tamtam if you already declared, you can you use varchar(30) instead of 10 to fit time stamp too.
I changed it to (ServiceEntry.ClosedDate between ''' + convert(varchar(30), StartDate, 120) + ''' and ''' + convert(varchar(30), EndDate, 120) + ''') and still the error Conversion failed when converting date and/or time from character string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.