Solved

Combine 2 Dates into 1

Posted on 2008-06-11
6
271 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
[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
  • 3
6 Comments
 
LVL 143

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 143

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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 143

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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