Solved

Microsoft SQL Server Column Function

Posted on 2012-03-09
5
236 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
5 Comments
 
LVL 7

Accepted Solution

by:
micropc1 earned 300 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 100 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 100 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

809 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