Solved

How to update this datetime to new table ?

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now