Avatar of r_lynn
r_lynn
Flag for United States of America asked on

SQL Time Format

OK, I am banging my head on something which should be simple!
I am creating a SQL statement to display information from an appointments database.  The data fiels I am reading is formatted 'yyyy-mm-dd hh:mm:sc.ms" (1900-01-01 09:30:00.000).  The date extraction was easy enough for me but the time format I need is eluding me.  The time needs to be formatted 'hh:mm AM(or PM)'


SELECT        PatientID, DoctorID, OfficeID, CONVERT(varchar(10),Date,101)AS Date, time, DurationInMinutes, Cancelled, NewPatientLast, NewPatientFirst, NewPatientMI, Comments
FROM            Appointments
WHERE        (Date = CONVERT(DATETIME, '2011-09-30', 102)) AND (PatientID > 0)
ORDER BY OfficeID 

-------------------------------------------------------------
RESULTS ARE:
PatientID	DoctorID	OfficeID	Date	time	DurationInMinutes	Cancelled	NewPatientLast	NewPatientFirst	NewPatientMI	Comments
75044	5	1	09/30/2011	1900-01-01 07:55:00.000	5	0	NURSE                     	NULL	NULL	NO LASER HAIR IN SEPT

Open in new window

Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
r_lynn

8/22/2022 - Mon
Lee

select substring(convert(varchar(20), GetDate(), 9), 13, 5) + ' ' + substring(convert(varchar(30), GetDate(), 9), 25, 2)

gets you the time in AM/PM rather than 24hour.
r_lynn

ASKER
Cool, this does get me the correct format which is a big part of my issue, but it is grabbibg the system date.  I replace sysdate with the name of the field 'Time' and it fails with
Msg 195, Level 15, State 10, Line 2
'time' is not a recognized built-in function name.
Lee

Whatever you replace the getdate() with should be your column name where the datetime is. What is the column name that contains the datetime from which you want the date?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
r_lynn

ASKER
The column name is "Time"
ASKER CERTIFIED SOLUTION
Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Lee

Personally I wouldn't use reserved words like date as a column name, but that is my personal preference.
r_lynn

ASKER
Thanks for your help, my experience , as you can tell, is a bit weak and I have spent some time trying to resolve this knowing that it would be simple for someone with experience.

Thanks Again,
-Randy
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Lee

You're very welcome :)
r_lynn

ASKER
I agree on the reserved word, but this is what I inherited, thanks again!