Solved

SQL - Syntax help String Manipulation - SQL Server 2005

Posted on 2011-03-23
4
252 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 101
How to place a condition in a filter criteria in t-sql (#2)? 10 41
Restrict result set 1 33
SQL join help to a thrid table 51 75
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 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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

16 Experts available now in Live!

Get 1:1 Help Now