Solved

SQL Server - Column Update

Posted on 2013-01-02
8
358 Views
Last Modified: 2013-01-02
Hello All,

I have a SQL Server Question. I am not sure which the best way to do it is. Now the requirement:
Every time, a new row(s) is inserted to my table xyz, then there is column in that table, called [update time], that gets the time of update in the format below…

For example – server Table
col1      col2      col3      updateTime
dsas      sdads      sdad      12/04/2012 1.45 PM
dsas      sdads      sdad      12/04/2012 1.50 PM


Is this difficult to accomplish?

Thanks
R
0
Comment
Question by:Rayne
8 Comments
 

Author Comment

by:Rayne
ID: 38735887
Also, [updateTime] column has to be of any specific format for achieving this?
0
 
LVL 15

Assisted Solution

by:Anuj
Anuj earned 200 total points
ID: 38735903
Add a default constraint to UpdatedTime, so the default value will get inserted in to the updatedTime column when you did not supply the value for this column in your insert statement.

ALTER TABLE <TableName>
ADD CONSTRAINT DF_UpdatedTime_GetDate DEFAULT GETDATE() FOR UpdatedTime

You can also change the format if your UpdateTime is varchar column
ALTER TABLE <TableName>
ADD CONSTRAINT DF_UpdatedTime_GetDate DEFAULT CONVERT(VARCHAR,GETDATE(),101) FOR UpdatedTime
0
 

Author Comment

by:Rayne
ID: 38736067
Perfect Anujnb,

That works but I am getting the date format as below:
Jan  2 2013  2:18AM

is it possible to get it like this?
01/02/2013 2:18AM

Thanks for your help so far :)
0
 
LVL 5

Assisted Solution

by:RehanYousaf
RehanYousaf earned 100 total points
ID: 38736080
For different time formats, you can try the following link: http://www.sql-server-helper.com/tips/date-formats.aspx
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 200 total points
ID: 38736086
try this for format as you want

select CONVERT(VARCHAR(10), GETDATE(), 101) + ' ' + RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7)

Try this when you are retriving the data like

Select col1 , col2 , select CONVERT(VARCHAR(10), UpdateTime , 101) + ' ' + RIGHT(CONVERT(VARCHAR, UpdateTime , 100), 7)
0
 

Author Closing Comment

by:Rayne
ID: 38736095
Thank you All for your excellent 5 ***** help :)
For your effort.
Greatly appreciate it
0
 
LVL 15

Expert Comment

by:Anuj
ID: 38736104
If this is a date time column I recommend you to Insert the date time value with default format and while retrieving the values convert to the required format. This method has the advantage that you can do date manipulations to the date time column with out the requirement of any conversion and of-course the performance adv also.

SELECT CONVERT(VARCHAR,GETDATE(),101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20), GETDATE(), 100),7))

SELECT CONVERT(VARCHAR,UpdateTime(),101)+' '+ LTRIM(RIGHT(CONVERT(VARCHAR(20), UpdateTime, 100),7))
FROM <YourTable>
0
 

Author Comment

by:Rayne
ID: 38736108
thank you Anuj :)

Sure I will keep that in mind
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now