Get hour greater than 6 hour SQL Qurey

anyone?

update tbluser set esdauth = '0' where userid = (SELECT [IDNumber], [Result], dateadd(HOUR, +6, [Date]) AS Date
FROM view_esd_reco
where 
convert(varchar(13),dateadd(HOUR, +6, [Date]),121) = convert(varchar(13),getdate(),121))

Open in new window


I have table "tbluser" with columns (userid, empname, esdauth) and a views table "view_esd_reco" with columns (IDNumber, Date, Name, Result)

Sample Table Photo:



I want to update tbluser (esdauth) to from "1" to "0" but depends on view_esd_reco 's date/Time where date greater than 6 Hours

Thank you.
Rowel VirgoVisual Studio .NETAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Since it is not clear whether you want the date column should be greater than 6 hours from current time or 6 hours before current time..
So, change the 6 to -6 as required..
update t1
set esdauth = '0' 
FROM tbluser t1
JOIN view_esd_reco t2 on t1.userid = t2.[IDNumber]
WHERE [Date] > DATEADD(hh, 6, getdate())

Open in new window

0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Added one more WHERE condition for safety reasons..
update t1
set esdauth = '0' 
FROM tbluser t1
JOIN view_esd_reco t2 on t1.userid = t2.[IDNumber]
WHERE [Date] > DATEADD(hh, 6, getdate())
AND t1.esdauth = '1' 

Open in new window

0
Rowel VirgoVisual Studio .NETAuthor Commented:
view_esd_reco has a date if the date/time is greater than 6 hour from now then it will update the tbluser's esdauth column to 0 else it remains to its status 0 or 1, I got error in the above query sir, thank you for the response.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

PortletPaulfreelancerCommented:
Do you need GREATER THAN or EQUAL? (I'm not sure, it may be needed) If not then just use equal, but try a select query first
      SELECT
            [IDNumber] , [Date], DATEADD(HOUR, 6, [Date]) 
      FROM view_esd_reco
      WHERE DATEADD(HOUR, 6, [Date]) >= cast(GETDATE() as date)

Open in new window


If that works out, then an update would look like this I think.

UPDATE tbluser
SET esdauth = '0'
WHERE userid IN (
      SELECT
            [IDNumber]
      FROM view_esd_reco
      WHERE DATEADD(HOUR, 6, [Date]) >= cast(GETDATE() as date)
      )

Open in new window


NB. It really isn't necessary to convert dates into varchar strings for comparison purposes.
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
>> I got error in the above query sir

Above is a valid TSQL syntax and kindly let me know what is the error you have received..
0
Rowel VirgoVisual Studio .NETAuthor Commented:
12:18:38      update t1 set esdauth = '0'  FROM tbluser t1 JOIN view_esd_reco t2 on t1.userid = t2.[IDNumber] WHERE [Date] > DATEADD(hh, 6, getdate()) AND t1.esdauth = '1'      


Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM tbluser t1 JOIN view_esd_reco t2 on t1.userid = t2.[IDNumber] WHERE [Date] ' at line 3      0.000 sec


this one sir
0
Rowel VirgoVisual Studio .NETAuthor Commented:
same errors??
0
Rowel VirgoVisual Studio .NETAuthor Commented:
If view table view_esd_reco record date/time of the user reached the 6 hours limit, then it will update tbluser esdauth to 0

this is how I create the view

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `elitescanning`@`%` 
    SQL SECURITY DEFINER
VIEW `view_esd_reco` AS
    SELECT 
        `a`.`IDNumber` AS `IDNumber`,
        `a`.`Date` AS `Date`,
        `b`.`Name` AS `Name`,
        `b`.`Result` AS `Result`
    FROM
        (((SELECT 
            `eliteprototype`.`esd_reco`.`IDNumber` AS `IDNumber`,
                MAX(`eliteprototype`.`esd_reco`.`Date`) AS `Date`
        FROM
            `eliteprototype`.`esd_reco`
        GROUP BY `eliteprototype`.`esd_reco`.`IDNumber`)) `a`
        JOIN `eliteprototype`.`esd_reco` `b` ON (((`a`.`IDNumber` = `b`.`IDNumber`)
            AND (`a`.`Date` = `b`.`Date`))))

Open in new window


I got this from internet but i think no need  to show how I create the table views or the view_esd_reco
0
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Got it, you are using MySQL.. try this below..
update tbluser 
set esdauth = '0' 
where userid IN (SELECT [IDNumber]
FROM view_esd_reco
where convert(varchar(13),[Date],121) > convert(varchar(13),dateadd(HOUR, +6, getdate()),121))

Open in new window

0
Rowel VirgoVisual Studio .NETAuthor Commented:
update tbluser
set esdauth = '0'
where userid IN


(select (IDNumber) from view_esd_reco
 where Date <= CURRENT_TIMESTAMP  - INTERVAL '6' HOUR)



I Got It
0
PortletPaulfreelancerCommented:
This should work in MySQL:
     SELECT
            `IDNumber` , `Date`, DATE_ADD(`Date`, INTERVAL 6 HOUR) Plus6
      FROM view_esd_reco
      WHERE DATE_ADD(`Date`, INTERVAL 6 HOUR)  >= CURDATE()

Open in new window

If that is the correct logic, then:
UPDATE tbluser
SET esdauth = '0'
WHERE userid IN (
      SELECT
            `IDNumber`
      FROM view_esd_reco
      WHERE DATE_ADD(`Date`, INTERVAL 6 HOUR)  >= CURDATE()
      )

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.