Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to update this datetime to new table ?

Posted on 2014-01-15
5
Medium Priority
?
280 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 48

Expert Comment

by:Dale Fye
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 800 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 1200 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

Industry Leaders: 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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

688 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