Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to update this datetime to new table ?

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

876 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