Solved

SQL - Syntax help String Manipulation - SQL Server 2005

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

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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

18 Experts available now in Live!

Get 1:1 Help Now