Solved

How to update this datetime to new table ?

Posted on 2014-01-15
5
275 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
[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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

734 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