I have a table with 2 columns dateRecieved and dateDue. I know how to make the dateNow column show today's date by default by using:
DEFAULT GETDATE()
The user can then modify the dateRecieved column to whatever date they want.
After the user modifies the dateRecieved column; how do I set up the dateDue column so that it, by default, shows a date x days away from dateRecieved?
I looked at this answer but I need to refer to another column.
Need to add constraint: date plus 10 days
Is there a way that dateRecieved can modify its value automatically when dateRecieved changes?
The user should be able to override the default values in both these columns.
I'm using the W3 schools sql tutorial but it didn't have and answer I couldn't find one on stackoverflow either
http://www.w3schools.com/sql/sql_default.asp
use current date as default value for a column
Thanks
So when you say the "user modifies" you are talking about an UPDATE.
DEFAULT values are relevant to INSERT statements only.
It is true you can accomplish whatever automagical UPDATE change you want via triggers but as a beginner, I would squarely advise you away from triggers. They are rarely implemented correctly and make understanding when things go wrong more difficult.
What you want to do is INSERT your row(s) (allowing any DEFAULT values to "happen") and then UPDATE your row(s) with the values you intended.
It sounds like what you're looking for is a trigger that waits until the update is finished, then the dateDue automatically updates.
This answer may point you in the right direction:
How to: Create trigger for auto update modified date with SQL Server 2008
Related
I have a computed column known as TotalTime. This column is populated using the DATEDIFF function from two datapoints that we are getting entered in from Microsoft PowerApps - StartTime and EndTime.
However, sometimes it would be nice to be able to make adjustments to the calculation. For example, if the PowerApps user selects a certain control, it would add five minutes to TotalTime.
This doesn't necessarily have to come from PowerApps itself - but could be a stored proc or function. What is the cleanest way to do this without having to break the computed column?
Add a new column (TotalTimeAdjustment) reserved for admin adjustment. This will be normally NULL (or can be defaulted to 0). Modify your calculated column to include TotalTimeAdjustment column as the 3rd input alongside StartTime and EndTime. Whenever an adjustment is needed the admin can update TotalTimeAdjustment for the relevant record(s).
Is it possible to store a variable in a SQL table which will only take effect once a certain date is reached? The variable is the amount of days that would be added to a date to create a "TargetDate", this variable can be changed by user input but must have an "EffectiveDate" ?
You can certainly create a configuration table that you store config data in. In your case, one of the items would be 'EFCTV_BUFFER' as the value in the key column, and (for an example) '5' as the number of days in the value column. Then you can reference that key value to select the buffer days value, and add that to whatever date you want.
This allows you to modify it at any time as requested.
You would reference this table in an insert/update trigger on your table where you store your dates. I would suggest having two dates so that the calculation is only done once, unless you need the calculation to be dynamic based upon the current 'EFCTV_BUFFER' configuration value.
I entered many data in MongoDB on different dates,
for example
from January, each day I entered data, how to know which date I started data insertion process
I think that there is no way to find that out, a workaround for that is you can alter your table and add a timestamp column, with default value now().
That let you see when future inserts are created.
I have a feature in my application where a user can opt to UNsubscribe itself. When this happens, there is no change made to the other fields in database and only user's Subscription flag is unchecked. I am doing this by getting a datarow and settingone of the field values and then updating the datarow in the table.
This is working fine for all of the recently created records, however some of the old records have a blank value set for a date field which is now mandatory. Hence when I try to unsubscribe old record of this type, it tries to retrieve null value from the database and then update the same back resulting in an error - "Cannot insert null value in Date Field"
First, if you have a table with a null value in a mandatory column, you would be better served to run an update query on the table that will enter a default value for this mandatory/required column. Wayne G. Dunn mentioned this in the comments to your question. For example:
UPDATE [TABLE1] SET [datefield] = '4/28/1949' where [datefield] = null
**Be sure to backup your data prior to running this kind of query and test with a test table before you execute the query on production data.
Second, if you are doing an update to a row and setting the checkbox value to true, you can review any columns that are required and set their default value in the update query. This makes the query more complex and more difficult to support.
I advise you go with the first option and run an update query on the table. The second option adds unnecessary code to your query that updates the checkbox.
If you post some query examples and table structure, you will get more specific answers. You may consider editing your question to include these details.
I would like to create an control on embedded on a continuous form of which its activation event creates a new record on the form and inserts today's date in the date field. I was thinking to use an SQL query to create the record but still am having difficulty.
Maybe you should try this...
In the table being inserted into, add a field called "date_entered" and have its default
value set to "Now()". This will give you a timestamp (knowledge of when the record was entered).
Doing it this way is the easiest way, and then you don't have to write any extra code.