Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2017
    Posts
    1,679

    CAST() problem

    Today I had a problem with a query, where I needed to calculate remaining days to given date.



    In table, I have a date field (date, not datetime!), e.g. DeliveryDate. In query (run from stored procedure), the number of days remaining to DeliveryDate from today's date must be calculated. I.e. for start:
    SELECT ..., DeliveryDate, CAST((DeliveryDate - GetDate()) AS smallint) AS RemaininigDays, ...
    This works, but as GetDate() returns datetime, then all remaining days are shifted. -1 for today, 0 for tomorrow, etc.

    I could calculate todays date as date value without problems
    CAST(GetDate() AS Date)
    - the time part was removed when this expression is returned as result field, but when using the same conversion in query expression
    SELECT ..., DeliveryDate, CAST((DeliveryDate - CAST(GetDate() AS Date)) AS smallint) AS RemaininigDays, ...
    I got an error. And when I instead created a variable @Today = CAST(GetDate() AS Date), and used this in query instead
    SELECT ..., DeliveryDate, CAST((DeliveryDate - @Today) AS smallint) AS RemaininigDays, ...
    , then I got the same error again!

    The result was same, when instead CAST I tried CONVERT. Probably the cause is, that some remaining days will have value 0 (and <0 until converted to smallint, but why did it work in topmost query then?) .

    Currently I simply added 1 to RemainingDays, but this doesn't feel as correct solution!
    Any ideas?

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If you use T-Sql

    DATEDIFF(Day,DeliveryDate,Getdate()) as RemainingDays

    Do you get the correct results?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Join Date
    Apr 2017
    Posts
    1,679
    Have to check this in next week (I'm working on half-time from this year, and for this week my hours are done).

    Usually I use UDF's created in a separate utilities DB on my SQL Server (there is one which works like DateSerial() in MS Office), when designing such queries, but for this case I wanted the script which can run on any other SQL Server. Your solution looks promising!

  4. #4
    Join Date
    Apr 2017
    Posts
    1,679
    Quote Originally Posted by Minty View Post
    DATEDIFF(Day,DeliveryDate,Getdate()) as RemainingDays
    Do you get the correct results?
    Yes, the results were correct! Thanks!

    The weird thing is, that today the query with CAST() too didn't return negative values? Unfortunately I didn't save exactly same query which returned wrong day numbers, so no I don't know what did get wrong way in last week!

    Meanwhile I had an idea I'd like to check out, but I don't want to risk it on our live SQL Server. I'll post a new thread about it now.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Crosstab or Join Query to cast data
    By chelonidae in forum Queries
    Replies: 14
    Last Post: 05-09-2018, 11:00 AM
  2. Replies: 6
    Last Post: 12-06-2017, 07:59 AM
  3. UPDATE query with CAST function
    By hsavignac in forum Queries
    Replies: 1
    Last Post: 12-07-2015, 12:22 PM
  4. Replies: 6
    Last Post: 11-05-2015, 06:14 AM
  5. Replies: 1
    Last Post: 12-14-2012, 09:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums