Please Help with SELECT Statement for Smalldatetime values???

  • I have a column of data within my SQL Server table that is set to smalldatetime...

    In one of my java server pages I am trying to pull values from this column..using a select [date] from tbl where '"+date1+"' and '"+date2"'...

    But it is not working!

    I can not seem to find the best way to query these dates...when I look at the server, the dates show as a date & time...

    Could someone please assist me with this???

    Thank you

  • Select id_col, col1, col2 from dbo.YourTable where date_col between '" + varDateStart + "' and '" + varDateEnd + "'"

    It would be a good idea to use a stored proc to do this too...

  • Would you explain your code??

    This is what I have currently:

    select count([inquiry_type]) AS [Count], ([inquiry_type]) AS [Inquiry Type] FROM tblOAO Where oao_date2 BETWEEN '"+dayA+"' AND '"+dayB+"' GROUP BY [inquiry_type]

    dayA and dayB look like this '04/16/2005' and '04/17/2005'...but the server shows the values as 4/16/05 4:39:00 PM and 4/17/05 1:39:00 PM...

  • Hence the vartype dateTIME. In sql server, the time always comes with the date. The only thing you can do is to set the time to midnight. If dayA and dayB are set like this : '04/16/2005' and '04/17/2005' : it'll return all the records that have a datestamp of 04/16/05 .*.

  • How do I accomplish your suggestion?

  • Did you try runnning the query?

    What results did you get?

    What results did you want to get?

  • the total count is inaccurate???

    The query below is giving a wrong number based on a comparison from the SQL Server table...

    select count([inquiry_type]) AS [Count], ([inquiry_type]) AS [Inquiry Type] FROM tblOAO Where oao_date2 BETWEEN '"+dayA+"' AND '"+dayB+"' GROUP BY [inquiry_type]

    When I manually count the records in the server I get 47 records...but when I use the above query...I get 35...so it is evidently not pulling the correct data...

    What am I doing wrong??

  • As I was saying you are getting records only for the 16th NOT the 17th.

    You can change the query to this :

    where oao_date2 >= '" + dayA + "' and oao_date2 < dateadd(D, 1, Datediff(D, 0, '" + dayB + "'))"

  • Thank you very much...it is working!

Viewing 9 posts - 1 through 8 (of 8 total)

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