Solved

SQL - Syntax help String Manipulation - SQL Server 2005

Posted on 2011-03-23
4
255 Views
Last Modified: 2012-05-11
Hello experts,

I am really reaching on this one, but I have a scenario that I need help coding please, not sure if it can be done.  I have a table of appointments below, and there are two columns, both varchar(50) datatypes.  The time and date fields were accidentally merged, and I need to unmerge them.  I currently have:

table: appointments
appt_date             appt_time
null                        Appointment date: 02/28/2011
null                        08:00 AM
null                        09:00 AM
null                        10:00 AM
null                        11:00 AM
null                        Appointment date: 02/29/2011
null                        08:00 AM
null                        09:00 AM
null                        10:00 AM
null                        11:00 AM
null                        Appointment date: 02/30/2011
null                        08:00 AM
null                        09:00 AM
null                        10:00 AM
null                        11:00 AM

and I need:

table: appointments
appt_date                                      appt_time
Appointment date: 02/28/2011      08:00 A
Appointment date: 02/28/2011      08:00 AM
Appointment date: 02/28/2011      09:00 AM
Appointment date: 02/28/2011      10:00 AM
Appointment date: 02/28/2011      11:00 AM
Appointment date: 02/29/2011      08:00 A
Appointment date: 02/29/2011      08:00 AM
Appointment date: 02/29/2011      09:00 AM
Appointment date: 02/29/2011      10:00 AM
Appointment date: 02/29/2011      11:00 AM
Appointment date: 02/30/2011      08:00 A
Appointment date: 02/30/2011      08:00 AM
Appointment date: 02/30/2011      09:00 AM
Appointment date: 02/30/2011      10:00 AM
Appointment date: 02/30/2011      11:00 AM

update appointments
set appt_date ?

Thoughts?

Thanks!
0
Comment
Question by:robthomas09
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 425 total points
ID: 35202096
robthomas09,

How are the rows with just time values related back to a specific date?  

If the data is truly as you showed above and the times are consistent over all the dates, you may want to consider clearing the entire table.

e.g.,
truncate table appointments;

Open in new window


Then you can insert new values using a cross join like so:
insert into appointments(appt_date, appt_time)
select appt_date, appt_time
from (
   select '2011-02-28' as [appt_date]
   union select '2011-03-01'
   union select '2011-03-02'
) dates
cross join (
   select '08:00 AM' as [appt_time]
   union select '09:00 AM'
   union select '10:00 AM'
   union select '11:00 AM'
) times
;

Open in new window


If you have a lot of dates (i.e., a range of dates), then you can use a numbers or dates table as the listing of dates -- using CONVERT to format the date as VARCHAR however you like.  Similarly, you can probably build the times in this same fashion.  If you move to SQL 2008, you can consider DATE and TIME data types.  For SQL 2005, you can consider having two DATETIME columns.  One with date at midnight to signify the day and another for time whose date can be ANSI date 0 (e.g., 1900-01-01 08:00:00, 1900-01-01 09:00:00, etc.).  You can use the same CONVERT functions to display as date only or time only, but then get the added benefit of date validation and sorting.  2/30 is probably just sample data, but for example is a bad date that would not be valid input to a true datetime field.

Just a thought.

Best regards and happy coding,

Kevin
0
 
LVL 5

Assisted Solution

by:bitref
bitref earned 50 total points
ID: 35206125
You may use a cursor to pass by the old rows one by one.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 25 total points
ID: 35207699
What does 08:00A mean?
0
 

Author Closing Comment

by:robthomas09
ID: 35244209
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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