Solved

SQL Server - Column Update

Posted on 2013-01-02
8
364 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2012 Report Builder 3.0 query 2 24
SQL Percentage Formula 7 33
When are cursors useful? 8 62
Error converting data type varchar to numeric. 4 11
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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

751 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