Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Datetime field and Concat in update

Author  Topic 

choideyoung
Starting Member

10 Posts

Posted - 2014-05-19 : 15:29:03
Need assistance in writing a script that will update a field by concatenating 3 columns while also specifying the date format in the new field


Here is what I want to create new Field of(adm,Disch,LastName)


Sample of current data and Format

Admission date
Jan 15 2013 12:00AM


Discharge Date
Jan 20 2013 12:00AM


Last Name
Smith


What current “New Field” looks like.

Jan 15 2013 12:00AM Jan 20 2013 12:00AMSmith

What New Field” needs to look like.

11520131202013Smith

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-19 : 15:39:03
You should not be creating a new column like this. If you need this for application purposes, then return the 3 columns separately and then do the concatenation in your application. If you do decide to create this column, you should do this as a computed column.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-05-20 : 08:12:06
May be this...

DECLARE @Admission_date DATETIME='Jan 15 2013 12:00AM'
,@Discharge_Date DATETIME='Jan 20 2013 12:00AM'
,@Last_Name VARCHAR(1024)='Smith'
SELECT REPLACE(CONVERT(VARCHAR,CAST(@Admission_date AS DATETIME),110),'-','')+REPLACE(CONVERT(VARCHAR,CAST(@Discharge_Date AS DATETIME),110),'-','')+@Last_Name




---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page
   

- Advertisement -