Solved

re-converting dateparts to a single date time

Posted on 2011-09-28
5
214 Views
Last Modified: 2012-05-12
I have split a date into 3 parts (day, month & year).

Currently I have these displayed (via select statement) as x3 columns.

How do I convert this back to a single column (a full datetime column entry) ?
,datepart(day,SetupTime) as 'DAY' 
,datepart(month,SetupTime) as 'MTH'
,datepart(year,SetupTime) as 'YEAR'

Open in new window

0
Comment
Question by:amillyard
[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
  • 3
5 Comments
 
LVL 8

Expert Comment

by:rushShah
ID: 36715131
try this,

SELECT Convert(DateTime, datepart(month,SetupTime) , datepart(day,SetupTime),
datepart(year,SetupTime) )
0
 

Author Comment

by:amillyard
ID: 36715191
Hi rushShah,  am getting:  Incorrect syntax near ','.  (after the datepart(day,SetupTime), )
0
 
LVL 25

Accepted Solution

by:
TempDBA earned 500 total points
ID: 36715230

SELECT DATEPART(DAY,SetupTime),
DATEPART(MONTH,SetupTime),
DATEPART(YEAR,SetupTime),
CONVERT(DATETIME,(CAST(DATEPART(MONTH,SetupTime) AS VARCHAR)  +'-' + CAST(DATEPART(DAY,SetupTime) AS VARCHAR) + '-' + CAST(DATEPART(YEAR,SetupTime) AS VARCHAR)) )

0
 

Author Comment

by:amillyard
ID: 36715244
this is the scripting context applying your suggestion
USE [db1]

DECLARE @endDate   datetime
DECLARE @startDate datetime

SET @startDate = '2011-09-01 00:00:00.000'
SET @endDate   = '2011-09-30 23:59:59.999'
	    
select cast( Cast(isnull(SUM(ChargeableAmount), 0) AS MONEY) / 100000 AS MONEY) AS 'TOTAL'
	   ,Convert(DateTime, datepart(month,SetupTime) , datepart(day,SetupTime),
datepart(year,SetupTime)
   
from [CDRData] 

WHERE (Direction = 1) 
	AND (COSID =95) 
	AND (SetupTime BETWEEN @startDate AND @endDate)
      
group by datepart(day,SetupTime), datepart(month,SetupTime), datepart(year,SetupTime)
order by datepart(day,SetupTime), datepart(month,SetupTime), datepart(year,SetupTime)

Open in new window

0
 

Author Closing Comment

by:amillyard
ID: 36715316
TempDBA:  that works great :-)  many thanks
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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