Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Server Date Problem

SQL Server Date Problem

Scheduled Pinned Locked Moved Database
databasehelpsql-serversysadminquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sim_san
    wrote on last edited by
    #1

    Hi Geeks, I am quite new to SQL Server.Well I have a problem.. Could someone help me ?? This is my Query: sql="SELECT Id,Name, Age,City, AdmDate FROM IP LEFT JOIN MDoc ON IP.DoId=MDoc.DoId WHERE AdmDate='11/15/2003' ORDER BY IP_Id" Now in the IP table there are records containing date values i.e. AdmDate='15/11/2003' ;these date values r in small date format with precision to seconds. Now I want All the records of a paticular date irrespective of their time Value. -SimSan

    M A 2 Replies Last reply
    0
    • S sim_san

      Hi Geeks, I am quite new to SQL Server.Well I have a problem.. Could someone help me ?? This is my Query: sql="SELECT Id,Name, Age,City, AdmDate FROM IP LEFT JOIN MDoc ON IP.DoId=MDoc.DoId WHERE AdmDate='11/15/2003' ORDER BY IP_Id" Now in the IP table there are records containing date values i.e. AdmDate='15/11/2003' ;these date values r in small date format with precision to seconds. Now I want All the records of a paticular date irrespective of their time Value. -SimSan

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      I typically use

      SELECT Id,Name, Age,City, AdmDate
      FROM IP LEFT JOIN MDoc
      ON IP.DoId=MDoc.DoId
      WHERE AdmDate >= '2003-11-15' AND
      AdmDate < DATEADD( Day, 1, '2003-11-15' )
      ORDER BY IP_Id

      Of course, typically I use this is in a stored procedure where the literal date is replaced by a parameter, where using DATEADD makes a bit more sense.

      S 1 Reply Last reply
      0
      • M Mike Dimmick

        I typically use

        SELECT Id,Name, Age,City, AdmDate
        FROM IP LEFT JOIN MDoc
        ON IP.DoId=MDoc.DoId
        WHERE AdmDate >= '2003-11-15' AND
        AdmDate < DATEADD( Day, 1, '2003-11-15' )
        ORDER BY IP_Id

        Of course, typically I use this is in a stored procedure where the literal date is replaced by a parameter, where using DATEADD makes a bit more sense.

        S Offline
        S Offline
        sim_san
        wrote on last edited by
        #3

        This works fine...Thanks for the tip. Well...I wanted to clarify if SQL Server treats Dates in American Date format.If it does,why this date format ('yyyy-mm-dd') is in use. -SimSan

        M 1 Reply Last reply
        0
        • S sim_san

          This works fine...Thanks for the tip. Well...I wanted to clarify if SQL Server treats Dates in American Date format.If it does,why this date format ('yyyy-mm-dd') is in use. -SimSan

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          Because I'm in the UK ;) SQL Server interprets literal dates in queries using the user's default locale - even in stored procedures, IIRC. You could write '10/11/2003' to be interpreted as October 11th, but if your user is using the English (UK) locale, it will be interpreted as 10 November. "SQL Server evaluates datetime constants at run time. A date string that works for the date formats expected by one language may be unrecognizable if the query is executed by a connection using a different language and date format setting." Using Date and Time Data[^] The ISO format yyyy-mm-dd is supposed to be supported in all locales with the same meaning. On re-reading Writing International Transact-SQL Statements[^], it actually looks like you should use unseparated dates (format yyyymmdd) to ensure correct interpretation. So I've been getting it wrong. :-O

          S 1 Reply Last reply
          0
          • S sim_san

            Hi Geeks, I am quite new to SQL Server.Well I have a problem.. Could someone help me ?? This is my Query: sql="SELECT Id,Name, Age,City, AdmDate FROM IP LEFT JOIN MDoc ON IP.DoId=MDoc.DoId WHERE AdmDate='11/15/2003' ORDER BY IP_Id" Now in the IP table there are records containing date values i.e. AdmDate='15/11/2003' ;these date values r in small date format with precision to seconds. Now I want All the records of a paticular date irrespective of their time Value. -SimSan

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            You could use SELECT Id,Name, Age,City, AdmDate FROM IP LEFT JOIN MDoc ON IP.DoId=MDoc.DoId WHERE AdmDate BETWEEN '11/15/2003 00:00' AND '11/15/2003 23:59' ORDER BY IP_Id"

            1 Reply Last reply
            0
            • M Mike Dimmick

              Because I'm in the UK ;) SQL Server interprets literal dates in queries using the user's default locale - even in stored procedures, IIRC. You could write '10/11/2003' to be interpreted as October 11th, but if your user is using the English (UK) locale, it will be interpreted as 10 November. "SQL Server evaluates datetime constants at run time. A date string that works for the date formats expected by one language may be unrecognizable if the query is executed by a connection using a different language and date format setting." Using Date and Time Data[^] The ISO format yyyy-mm-dd is supposed to be supported in all locales with the same meaning. On re-reading Writing International Transact-SQL Statements[^], it actually looks like you should use unseparated dates (format yyyymmdd) to ensure correct interpretation. So I've been getting it wrong. :-O

              S Offline
              S Offline
              sim_san
              wrote on last edited by
              #6

              Thanks again for your info..U've got it right!!! -SimSan

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups