1

Most of my tables have four fields that include:

Created datetime not null default = GetDate()
CreatedBy varchar(50) not null default = suser_sname()
Updated datetime null
UpdatedBy Varchar(50) null

For data that I display in a grid, I want to combine these fields to show the "Last Edit" in the form of "datetime" by "user".

Knowing that my Created and CreatedBy fields will never be null, Coalesce seems to be the answer but I'm getting errors with the following:

,Convert(varchar(20),Coalesce(i.Updated,i.Created) ,120) + ' by ' + Coalesce(i.UpdatedBy,i.Created) as [LastEdit]

2 Answers 2

3

In all likelihood, the problem is the second coalesce(). I am guessing you mean:

(Convert(varchar(20), Coalesce(i.Updated, i.Created), 120) + ' by ' +
 Coalesce(i.UpdatedBy, i.CreatedBy)
--------------------------------^
) as [LastEdit]

You were trying to combine a date with a string, which can cause a problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gordon - dumb mistake on my part. Created needs to be CreatedBy in the second coalesce.
1

Be wary of SQL Server's Concatenate Null Yields Null setting. You may want to either set it to off during your query execution. Or if you have SQL 2012 or higher, use the Concat() function. http://msdn.microsoft.com/en-us/library/hh231515.aspx - concat http://msdn.microsoft.com/en-us/library/ms176056.aspx - set concat_null_yields_null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.