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
Solved

How to update this datetime to new table ?

Posted on 2014-01-15
5
272 Views
Last Modified: 2014-01-26
I have one column in our table [activity] with storing datetime like 2014-01-01 00:00:00.000

But now we have a request to convert this value to another new column [MthID] with the values will be like 201401 for each rows we have in the table

This MthID column will have to combine the vaule from employeeid, so it should look like


123456201401

where 123456 is employeid and 201401 is after converted from [activity] column
0
Comment
Question by:motioneye
5 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 39781897
Since the standard formats do not support your needed date format, you need to concatenate it by yourself

CAST(employeeid as varchar(6)) + DATEPART(yyyy,[activity]) + DATEPART(mm,[activity])

This would create your needed string.
0
 

Author Comment

by:motioneye
ID: 39781902
Hi,

I received error message like below.

select CAST(employeeid as varchar(50)) + DATEPART(yyyy,[activity]) + DATEPART(mm,[activity]) from MyTable


Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '2.00109e+007' to data type int.



But when I run only below

select DATEPART(yyyy,[activity month]) + DATEPART(mm,[activity month])
from My_Catw

the results really weird it just show 2015 for all rows
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39781963
Personally, I'd advise against combining data (EmployeeID and YrMonth) into a single field.  What is the reason you think you need to do that?  You can always concatenate them in a query,  But once you combine them you now have to maintain them as well.  If you need to be able to reference these three fields in your new table, put all three fields there, don't concatenate them.

I can understand the desire to format the year and month as a single field, but I would generally just recommend creating a view of the data with that computed field, rather than adding another field to your table.
0
 
LVL 9

Assisted Solution

by:sarabhai
sarabhai earned 200 total points
ID: 39782003
try this one ... .. .
SELECT CAST(employeeid AS VARCHAR(50)) + CAST(DATEPART(yyyy,[activity]) AS VARCHAR(10)) + CAST( DATEPART(mm,[activity]) AS VARCHAR(5))  FROM  MyTable
0
 
LVL 11

Accepted Solution

by:
Murfur earned 300 total points
ID: 39782907
Got my head scratching on this one but I got there in the end - the crunch was that the target mthid field had to be a BIGINT or VARCHAR for the statement to work as it threw a wobbly and told me it had overflowed the int column. Note to self: it pays to have hoofed in a couple of fresh coffees before one attempts to answer a 'quick question'... :)
UPDATE dbo.convert_date
SET mthid = CONVERT(varchar(10),DATEPART(year,activity)) + RIGHT('0' + RTRIM(DATEPART(MM,activity)),2) + CONVERT(varchar(10), emp_id);

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL convert date to string 4 34
Acces SQL Insert Problem 6 28
Location of Dynamics AX Service accounts in SQL 3 15
What is this datetime? 1 18
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

790 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