Solved

SQL Server - Column Update

Posted on 2013-01-02
8
365 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
[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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

632 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