Solved

Combine 2 Dates into 1

Posted on 2008-06-11
6
269 Views
Last Modified: 2010-03-20
In my select statement I have 2 dates and I tried using a (+ '-' +) to add the 2 fields together but it kept giving me this error   (The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.)  The following is my sql code, what I am trying to do is put the dates together and try to format them as 26-30 june 2008, right now they would show up as 26-june-2008 and 30-june-2008 in my labels on aspx page.  Is this possible or not????  Thanks

select      s.sidstrName_IND,
      s.sidstrST_ADDR,
      s.sidstrADDR_CITY,
      s.sidstrSTATES_US,
      s.sidstrZIP_CODE,
      s.sidstrGR_ABBR_CODE,
                     s.sidstrPAY_GR,
      cast(datediff(year, siddatPEBD, getdate()) as varchar(20)) as PGYrs,
      count(p.intAssignedPersonnelID),
      min(p.dtattendance) + '-' + max(p.dtattendance) as EDate    (Problem Area ??????)
from      cms.dbo.tblSIDPERS as s INNER JOIN
      tblAssignedPersonnel as p on s.sidstrSSN_SM = p.strSSN
where      p.bitPresent = 1 And
                     p.intDrillStatus = 2 And
      p.dtPayProcessed Is Not Null and
      p.dtPaid Is null and
      p.intUICID in (select intUICID from tblUIC where intTaskForceID = '13' and strUIC = 'PU3B1') and
                     p.strSSN = '474968040'
group by      s.sidstrName_IND, s.sidstrST_ADDR, s.sidstrADDR_CITY, s.sidstrSTATES_US, s.sidstrZIP_CODE,  s.sidstrGR_ABBR_CODE, s.sidstrPAY_GR, cast(datediff(year, siddatPEBD, getdate()) as varchar(20))
0
Comment
Question by:kdeutsch
  • 3
  • 3
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21763826
change:
min(p.dtattendance) + '-' + max(p.dtattendance)
into:
convert(varchar(10), min(p.dtattendance), 120) + '-' + convert(varchar(10), max(p.dtattendance) , 120)
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21763850
sorry, for the dd.mon.yyy:
convert(varchar(10), min(p.dtattendance), 106) + '-' + convert(varchar(10), max(p.dtattendance) , 106)

Open in new window

0
 

Author Comment

by:kdeutsch
ID: 21768533
OK, I found a little thing with sql that helps me with my problem of consolidating the 2 dates into one date range.  What I don't know is if it will eventually give me any hiccups in my program.  I took your coversion statement but for the first date changed the varchar to (2) so that it only shows the first 2 days of the date, then combined them so it looks like below.

convert(varchar(2), min(p.dtattendance), 106) + '-' + convert(varchar(18), max(p.dtattendance) , 106)

This gives me a date of 26-30 June 2008.  This solves my problem but will it give me hiccups latter on or is this good.  Thanks
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:kdeutsch
ID: 21768586
Ok I see what it does by doing it this way if a person only served one date it comes up as such
22-22 May 2008.  is there a way to format this more to take care of this problem.  Thanks
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21768965
question: what if the dates where 30/06 and 01/07 ? ...
would be 30-01 July 2008 ... not really simple :)
next step: 30/12/2007 - 01/01/2008 ....

I would put that "logic" eventually in a function...
0
 

Author Comment

by:kdeutsch
ID: 21769269
AH, see what you mean, if they go into another month it will look wrong, don't know that we have had that problem yet, as far as the year changeover, we won't have that problem because of FY constraints.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Sub-Query Help 22 62
SQL Insert parts by customer 12 32
Mysql Left Join Case 10 49
SQL Server - Set Field Values ito Zero Based on Related Table 4 22
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

775 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