Solved

Combine 2 Dates into 1

Posted on 2008-06-11
6
266 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

15 Experts available now in Live!

Get 1:1 Help Now