Solved

SQL Server - Column Update

Posted on 2013-01-02
8
363 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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

685 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