Domanda

I have been trying this for a while and so far research has not gotten me far other than I need to use something called DATEPART (I have no idea how to use this). My SQL Server is not the greatest.

I have declare @DueDate datetime and through a cursor @DueDate will always been the current row's DueDate (datetime) column. This so far works perfectly without issue.

Now what I am trying to do is get the hour out of @DueDate, check to see if it is 0, and if the hour is 0, set the @DueDate hour to midnight and then update the rows DueDate column with this @DueDate variable.

As stated above I have the cursor and variables all working, I just don't know how to get the hour from @DueDate, check what that hour value is, and update the @DueDate variable so its hour is now midnight. I know how the update the table, that is the easy part.

Thanks in advance!

È stato utile?

Soluzione

I think this does exactly what you want without using the cursor.

UPDATE [MyTable]
SET DueDate = DATEADD(dd, 1, DueDate) -- add one day (set to midnight)
WHERE DATEPART(hh, DueDate) = 0 -- do this for dates where hour is zero (midnight)

Note that you should avoid using cursors where possible. SQL development requires a different way of thinking. Don't think about iterations, think atomic. You can do most things in a single statement (one statement can be very long if complicated enough).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top