Results 1 to 10 of 10

Thread: [RESOLVED] SQL question

  1. #1

    Thread Starter
    Member tc101's Avatar
    Join Date
    Oct 2006
    Posts
    63

    Resolved [RESOLVED] SQL question

    Is there some way I can write a SQL select so that if a date field
    contains a certain date it is changed to another date, but all the
    other date fields remain the same?

    In other words:

    select name1, date1 from table

    With (If date1 = 1/1/1950 then date1 = 2/2/1952) somehow embeded in the
    SQL select.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: SQL question

    Something like...
    "UPDATE table SET date1 = #2/2/1952# WHERE date1 = #1/1/1950#"

  3. #3
    Hyperactive Member kxcntry99's Avatar
    Join Date
    Jun 2006
    Location
    Pennsylvania
    Posts
    342

    Re: SQL question

    tc101,

    There could be two ways of doing this. The first would be to use an update query to replace a certain date with another, something like:

    update table
    set datefield = '10/26/06'
    where datefield like '10/25/06'

    This would replace any cells where the date is 10/25/06 to 10/26/06. However, this would only occur once when you run the query. Which brings me to the second way of doing this...if you would like to have the date automatically swithced as a user imputs the data then you would need to use a trigger.

    Hope this helps

  4. #4
    Hyperactive Member kxcntry99's Avatar
    Join Date
    Jun 2006
    Location
    Pennsylvania
    Posts
    342

    Re: SQL question

    ahh..beat me to it leinad31

  5. #5

    Thread Starter
    Member tc101's Avatar
    Join Date
    Oct 2006
    Posts
    63

    Re: SQL question

    I can't do an update because I have to keep the original date in the table. I just want the results changes in one query.

    This is in Access (Jet) sql. I don't think you can use triggers.

    I may have to look for another solution if I can't make the change within a SQL query.

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL question

    Use the IIF function

    Select Name1, IIf(Date1=#01-Jan-1950#,#02-Feb-1952#, Date1)

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL question

    Does access support the Case construct?

    If so you can do this:

    Code:
    select name1, 
      CASE 
        WHEN date1 = '1/1/1950' THEN '2/2/1952'
        ELSE date1
      END as NewDate2
    from table
    But I don't work with Access, so I don't know if it'll support it or not.

    If not, then look to see if it supports IIF() .... I bet it does.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: SQL question

    And use an alias for the IIF expression.

  9. #9
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: SQL question

    Quote Originally Posted by techgnome
    Does access support the Case construct?

    If so you can do this:

    Code:
    select name1, 
      CASE 
        WHEN date1 = '1/1/1950' THEN '2/2/1952'
        ELSE date1
      END as NewDate2
    from table
    But I don't work with Access, so I don't know if it'll support it or not.

    If not, then look to see if it supports IIF() .... I bet it does.

    -tg
    Access doesn't do CASE but it does do IIF().

  10. #10

    Thread Starter
    Member tc101's Avatar
    Join Date
    Oct 2006
    Posts
    63

    Re: SQL question

    iff works.

    Thanks folks. You guys are life savers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width