When having an ultrasound done you're usually required to fast before the scheduled ultrasound test.
That means that you should not eat anything for as long as the doctor tells you to prior to the ultrasound appointment.
But you can drink anything you want to including coffee 2 hours before the ultrasound is to happen.
Just avoid drinking coffee, tea or anything else 2 hours before the ultrasound so stop drinking anything when it gets to 2 hours before the appointment for the ultrasound and you'll be fine.
Also 2 hours before your appointment time for the ultrasound you should drink 1 quart of clear liquid such as water, juice or even yes coffee.
Well coffee is not very clear but it's okay to drink it 2 hours before the ultrasound.