none
IF Exist to check if a record exists in table RRS feed

  • Question

  • Hi,

    I our DB we receive data imports daily and there is a column indicating the date of import for example:

    2014-03-01 00:00:00.000

    I would like to generate a simple IF statement to print on screen whether the latest data import has arrived on not. I was trying to check if the first 11 characters of the date is equal to the first 11 characters of GETDATE() function (I used the LEFT function to include only days/months/years):

    IF exists (SELECT left(date,11) FROM dwh.table where date= select left
    (getdate(),11)
    Print 'Record exits - Update' ELSE Print 'Record doesn''t exist - Insert'

    Can someone advise or propose some simpler code? All we need is a piece of code telling us if todays data is present in a column in a table.

    Thanks

    Suhi

    it does not work

    Thursday, October 23, 2014 5:42 PM

Answers

  • Try as follows

    IF exists (SELECT 1 FROM dwh.table where left(date,11)=  left (getdate(),11)) 
    Print 'Record exits - Update' ELSE Print 'Record doesn''t exist - Insert'


    Thanks
    Manish
    ___________________________________________________________________________________
    Please click Mark as Answer if my post solved your problem and click Vote as Helpful if this post was useful.

    • Marked as answer by Suhi100 Friday, October 24, 2014 8:40 AM
    Thursday, October 23, 2014 5:56 PM
  • IF exists (SELECT 1 
    FROM dwh.table 
    where date >= dateadd(dd,datediff(dd,0,getdate()),0) 
    and date < dateadd(dd,datediff(dd,0,getdate()),1) 
    )
    Print 'Record exits - Update' 
    ELSE 
    Print 'Record doesn''t exist - Insert'
    


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    • Marked as answer by Suhi100 Friday, October 24, 2014 8:40 AM
    Thursday, October 23, 2014 6:10 PM

All replies

  • Try as follows

    IF exists (SELECT 1 FROM dwh.table where left(date,11)=  left (getdate(),11)) 
    Print 'Record exits - Update' ELSE Print 'Record doesn''t exist - Insert'


    Thanks
    Manish
    ___________________________________________________________________________________
    Please click Mark as Answer if my post solved your problem and click Vote as Helpful if this post was useful.

    • Marked as answer by Suhi100 Friday, October 24, 2014 8:40 AM
    Thursday, October 23, 2014 5:56 PM
  • IF exists (SELECT 1 
    FROM dwh.table 
    where date >= dateadd(dd,datediff(dd,0,getdate()),0) 
    and date < dateadd(dd,datediff(dd,0,getdate()),1) 
    )
    Print 'Record exits - Update' 
    ELSE 
    Print 'Record doesn''t exist - Insert'
    


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    • Marked as answer by Suhi100 Friday, October 24, 2014 8:40 AM
    Thursday, October 23, 2014 6:10 PM