?
Solved

Microsoft SQL Server Column Function

Posted on 2012-03-09
5
Medium Priority
?
240 Views
Last Modified: 2012-03-13
I'm trying to set the default value in a table I'm creating to:
(dateadd(s,TotalTime*60, StartTime))

Where TotalTime is another column and is a Real.
And StartTime is another column and is a DateTime.

Syntax help please.

Thanks,
Ni
0
Comment
Question by:KnightWhoSaysNi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 1200 total points
ID: 37704176
I don't believe that's possible since SQL won't let you use field names in the default value.

You'll probably need to create a trigger to handle this. Something like...

CREATE TRIGGER
ON tableName
AFTER INSERT,UPDATE
AS
UPDATE tableName SET dateColumn = dateadd(s,TotalTime*60, StartTime) WHERE primaryKeyField IN (select primaryKeyField from inserted where dateColumn IS NULL)

Open in new window

0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 400 total points
ID: 37710137
A computed column may also be an option - Of course it depends on your requirment
Do you want the field to have a default value that can be overwritten if so desired by an app/users
If not a computed column might be an option
ALTER TABLE yourtable ADD yourcolumn (dateadd(s,TotalTime*60, StartTime))
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 37712709
As suggested previously you need a computed column, just a minor correction (no points please), for a computed column the AS is required as in:
ALTER TABLE yourtable ADD yourcolumn AS (DATEADD(s,TotalTime * 60, StartTime))
0
 
LVL 7

Expert Comment

by:micropc1
ID: 37712969
Yes - as bcunney said, It depends on your requirement. Since you said you want to set a "default" value I assume you're wanting to be able to insert data into the column.  You can't insert data into a computed column using an INSERT or UPDATE statement. If that's what you want you'll need a trigger. If you don't need to insert data then the computed column is the better and simpler option.
0
 

Author Closing Comment

by:KnightWhoSaysNi
ID: 37714653
Thanks for the input.  I think either solution provided would resolve the issue, but I went a little different direction.

I decided to use a stored procedure to perform the insert and declare the calculated variables in the stored procedure.

BEGIN TRY
      DECLARE @StartTime datetime;
      DECLARE @EndTime datetime;
      SET @EndTime = GETDATE();
      SET @StartTime = DATEADD(SS,@TotalTime*-60,@EndTime);
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question