[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

Return modified datetime data in Select...Case

I have a stored procedure that returns appointment data into a form.  Everything was working fine but I've been tasked with the following:

1. If the appointment is flagged as appAllDay = 1, the appointment start time (appDate) needs to display with a different time.  The physical data isn't touched or changed.

For example, 2010-02-05 08:00:00.000 needs to display as 2010-02-05 11:00:00.000 in my appTime column shown below.

The appDate field is a datetime column so I'm not sure if this is even possible.  Any assistance would be appreciated.

SELECT dbo.dbAppointments.appID,
(case appAllDay when 0 then appDate else
****
end) as appTime
FROM        
dbo.dbAppointments
WHERE dbo.dbAppointments.feeusrid = 7459
0
smsstech
Asked:
smsstech
2 Solutions
 
tlovieCommented:
Am I missing something, or would this work?

SELECT dbo.dbAppointments.appID,
(case appAllDay when 0 then appDate else
dateadd(hh, 11, convert(datetime, convert(varchar(8), appDate, 112), 112))
end) as appTime
FROM        
dbo.dbAppointments
WHERE dbo.dbAppointments.feeusrid = 7459

convert it from a date to a varchar, back to a date (to remove the time portion), then add 11 hours to it.  
0
 
Kevin CrossChief Technology OfficerCommented:
I agree, that approach should work, i.e., grabbing date at midnight and adding hours/minutes back to get to the time of day you want. As an aside, if you are using SQL Server 2008, there are new data types for DATE and TIME, separately; therefore, you may want to see if those are a better fit for your design. A NULL TIME for example might symbolize all day or at least you can always make time whatever you want based on appAllDay.

Kevin
0
 
smsstechAuthor Commented:
Thanks.  Fantastic work.

I don't deal with datetime in SQL very often so manipulation was new to me.  I appreciate the sample and the SQL Server 08 notes.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now