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. Trouble with date selection

Trouble with date selection

Scheduled Pinned Locked Moved Database
databasesql-serversysadminalgorithmstutorial
1 Posts 1 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I'm having trouble with this query, use to work in SQL Server 2008, but in 2012, I'm getting rows returned from various dates So I'm just trying to get all the orders from Dec 3, 2013, I'm passing integer values into the function 12 3 2013 and using parameters Day, Month, Year My Original SQL

    "SELECT * From CompletedOrders " & _
    "UNION " & _
    "SELECT * FROM CompletedOrdersHistory " & _
    "WHERE Day(OrderDate)=@TheDay " & _
    "AND Month(OrderDate)=@TheMonth " & _
    "AND Year(OrderDate)=@TheYear"

    Not really sure how to write this or what to search on. I have spent about an hour on this searching the internet. [update] I got this to work, but I have to use the greater than, so now I have to back date the date, which will get me trouble at the start of the month, guess I'll try looking up removing a day in SQL

    DECLARE @TheDate AS DATE;
    DECLARE @month AS INTEGER;
    DECLARE @day AS INTEGER;
    DECLARE @year AS INTEGER;

    set @month = 12;
    set @day = 2;
    set @year = 2013;
    set @TheDate = DateFromParts(@year, @Month, @Day);

    SELECT * From CompletedOrders
    WHERE OrderDate > @TheDate
    UNION
    SELECT * FROM CompletedOrdersHistory
    WHERE OrderDate > @TheDate;

    Well this is what I ended up with. It works, not sure of efficient it is

    DECLARE @startDate AS DATE;
    DECLARE @stopDate AS DATE;
    DECLARE @month AS INTEGER;
    DECLARE @day AS INTEGER;
    DECLARE @year AS INTEGER;

    set @month = 12;
    set @day = 3;
    set @year = 2013;
    set @startDate = DATETIMEFROMPARTS(@year, @Month, @Day, 0, 0, 0, 0);
    set @stopDate = DATETIMEFROMPARTS(@year, @Month, @Day, 23, 59, 59, 999);

    SELECT * From CompletedOrders
    WHERE OrderDate > @startDate AND OrderDate < @stopDate
    UNION
    SELECT * FROM CompletedOrdersHistory
    WHERE OrderDate > @startDate AND OrderDate

    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