Using Date in Where Condition

  • I Have issues in selecting records when using date in  my where clause1 )  I am trying to fetch records for a particular date

    SELECT [ID]
      ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MIDNAME]
      ,[REGNO]
      ,[LASTCHANGED]
       FROM [SRV-RVS].[dbo].[EMP]
    where LASTCHANGED = '2015/09/17'

    The above query returns  no result 

    2 ) but when i use the below where clause  

        where LASTCHANGEDLASTCHANGED BETWEEN '2015/09/17' AND '2015/09/18'

    It returned 81 records for  2015/09/17 

    How to retrieve records for the particular date using SQL query 

  • Your 1st query uses 2015/09/30 in the where clause and your 2nd has no mention of 2015/09/30 so 81 records for 2015/09/17 is probably correct.

    Thanks

  • NorthernSoul - Friday, October 6, 2017 7:38 AM

    Your 1st query uses 2015/09/30 in the where clause and your 2nd has no mention of 2015/09/30 so 81 records for 2015/09/17 is probably correct.

    Thanks

    Updated the question. 
    The first query was for 2015/09/17

  • aloshya - Friday, October 6, 2017 7:33 AM

    How to retrieve records for the particular date using SQL query 

    Roughly how you have done in your second WHERE clause, which will return all rows from 2015/09/17 00:00:00 to 2015/09/18 00:00:00 inclusive.  If you want to exclude 2015/09/18 00:00:00, do it like this:

    WHERE LASTCHANGED >= '2015/09/17 00:00:00'
    AND LASTCHANGED < '2015/09/18 00:00:00'

    John

  • aloshya - Friday, October 6, 2017 7:33 AM

    I Have issues in selecting records when using date in  my where clause1 )  I am trying to fetch records for a particular date

    SELECT [ID]
      ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MIDNAME]
      ,[REGNO]
      ,[LASTCHANGED]
       FROM [SRV-RVS].[dbo].[EMP]
    where LASTCHANGED = '2015/09/30'

    The above query returns  no result 

    2 ) but when i use the below where clause  

        where LASTCHANGEDLASTCHANGED BETWEEN '2015/09/17' AND '2015/09/18'

    It returned 81 records for  2015/09/17 

    How to retrieve records for the particular date using SQL query 

    Use

    WHERE LASTCHANGED >= '20150917' AND LASTCHANGED <'20150918'
    Don't use BETWEEN here, because it includes both endpoints.
    Note also the use of ISO date format for literal dates.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • John Mitchell-245523 - Friday, October 6, 2017 7:41 AM

    aloshya - Friday, October 6, 2017 7:33 AM

    How to retrieve records for the particular date using SQL query 

    Roughly how you have done in your second WHERE clause, which will return all rows from 2015/09/17 00:00:00 to 2015/09/18 00:00:00 inclusive.  If you want to exclude 2015/09/18 00:00:00, do it like this:

    WHERE LASTCHANGED >= '2015/09/17 00:00:00'
    AND LASTCHANGED < '2015/09/18 00:00:00'

    John

    I find it hard to use time in search field , is there any alternative and below @phil has shown one way to do it.

  • Phil Parkin - Friday, October 6, 2017 7:41 AM

    aloshya - Friday, October 6, 2017 7:33 AM

    I Have issues in selecting records when using date in  my where clause1 )  I am trying to fetch records for a particular date

    SELECT [ID]
      ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MIDNAME]
      ,[REGNO]
      ,[LASTCHANGED]
       FROM [SRV-RVS].[dbo].[EMP]
    where LASTCHANGED = '2015/09/30'

    The above query returns  no result 

    2 ) but when i use the below where clause  

        where LASTCHANGEDLASTCHANGED BETWEEN '2015/09/17' AND '2015/09/18'

    It returned 81 records for  2015/09/17 

    How to retrieve records for the particular date using SQL query 

    Use

    WHERE LASTCHANGED >= '20150917' AND LASTCHANGED <'20150918'
    Don't use BETWEEN here, because it includes both endpoints.
    Note also the use of ISO date format for literal dates.

    It returns the result for that particular date,
    even why we want to pass two dates for selecting records  for one particular date.

  • aloshya - Friday, October 6, 2017 7:55 AM

    It returns the result for that particular date,
    even why we want to pass two dates for selecting records  for one particular date.

    One particular date is actually one particular point in time.  That's why you need two of them: a beginning point and an end point.  If you want your dates to be discrete instead of continuous, change the data type of the column from datetime to date.

    John

  • John Mitchell-245523 - Friday, October 6, 2017 7:59 AM

    aloshya - Friday, October 6, 2017 7:55 AM

    It returns the result for that particular date,
    even why we want to pass two dates for selecting records  for one particular date.

    One particular date is actually one particular point in time.  That's why you need two of them: a beginning point and an end point.  If you want your dates to be discrete instead of continuous, change the data type of the column from datetime to date.

    John

    My date column is varchar,

  • aloshya - Friday, October 6, 2017 8:03 AM

    John Mitchell-245523 - Friday, October 6, 2017 7:59 AM

    aloshya - Friday, October 6, 2017 7:55 AM

    It returns the result for that particular date,
    even why we want to pass two dates for selecting records  for one particular date.

    One particular date is actually one particular point in time.  That's why you need two of them: a beginning point and an end point.  If you want your dates to be discrete instead of continuous, change the data type of the column from datetime to date.

    John

    My date column is varchar,

    You shouldn't be using varchar to store a date. Are you able to change this and use one of the date data types?

    Thanks

  • aloshya - Friday, October 6, 2017 8:03 AM

    My date column is varchar,

    Same difference.  It'll be implicitly converted to datetime.  This is an example of why tables should be designed with the correct data types.

    John

  • John Mitchell-245523 - Friday, October 6, 2017 8:10 AM

    aloshya - Friday, October 6, 2017 8:03 AM

    My date column is varchar,

    Same difference.  It'll be implicitly converted to datetime.  This is an example of why tables should be designed with the correct data types.

    John

    I can change that and usually, I am comfortable with datatypes except for date . : 🙂

  • aloshya - Friday, October 6, 2017 8:03 AM

    John Mitchell-245523 - Friday, October 6, 2017 7:59 AM

    aloshya - Friday, October 6, 2017 7:55 AM

    It returns the result for that particular date,
    even why we want to pass two dates for selecting records  for one particular date.

    One particular date is actually one particular point in time.  That's why you need two of them: a beginning point and an end point.  If you want your dates to be discrete instead of continuous, change the data type of the column from datetime to date.

    John

    My date column is varchar,

    Understand that with varchar data having datetime values, you have no chance of getting it right without converting it to at least a date value, and that will definitely make the query much slower than it would otherwise be, unless your data quantity is trivial.  Because of the potential for an invalid datetime value that can occur in a varchar column, continuing to use that data type is probably negligent, at the least.   As in terrible, horrible, no good, very bad idea.  If you can have that data type changed, you should, even if it means a lot of other work needs to happen.   It's a gaping hole that will eventually bite you in the butt far worse than this particular issue will at this point in time.   Further down the road, performance could become a rather serious issue as more and more data accumulates.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • aloshya - Friday, October 6, 2017 8:20 AM

    I can change that and usually, I am comfortable with datatypes except for date . : 🙂

    Please go ahead and change it. Future developers will thank you!
    Post any questions you may have around handling dates here and we will attempt to ease your discomfort 🙂
    If you have no need of a time component, use the DATE datatype, as John has already suggested. If you do that, your query to return rows for specific day becomes simpler:

    WHERE DATE = 'YYYYMMDD'

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • aloshya - Friday, October 6, 2017 8:20 AM

    John Mitchell-245523 - Friday, October 6, 2017 8:10 AM

    aloshya - Friday, October 6, 2017 8:03 AM

    My date column is varchar,

    Same difference.  It'll be implicitly converted to datetime.  This is an example of why tables should be designed with the correct data types.

    John

    I can change that and usually, I am comfortable with datatypes except for date . : 🙂

    I'd  suggest that your data is likely to contain time values in addition to the date.   You'll have to take a good close look at your data, and perhaps do the following query to see if any of your data has a problem:
    SELECT *
    FROM yourtable
    WHERE TRYCONVERT(datetime, yourcolumnname) IS NULL;

    Be sure to use your table name and your column name in place of what I coded.   If that query returns 0 rows, then you can probably safely change the data type in place, but for safety, probably a better idea to add a new datetime column, and then populate it using a CONVERT(datetime, existingcolumnname).

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

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

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