title: How to save an image from SQL Server to a file using SQL
body: <h2>Question</h2>
  
  <p>In SQL Server 2005, I have a table with images (data type: image). Using only SQL, how do I save an image as a file (on the same server that SQL Server is running). If I have to, I'll use SQL CLR, but I want to avoid that if possible.</p>
  
  <h2>Background</h2>
  
  <p>I want a SQL Server job to run no a schedule that calls a proc that will send emails with embedded images using SQL Server Database Mail like this: </p>
  
  <pre><code>exec msdb.dbo.sp_send_dbmail
  @profile_name = 'MyProfile',
  @recipients = 'bob@hotmail.com',
  @subject = 'hello',
  @file_attachments = 'C:\MyLogo.gif',
  @body=N'&lt;p&gt;Image Test&lt;/p&gt;&lt;img src="MyLogo.gif" /&gt;&lt;p&gt;See image there?&lt;/p&gt;', 
  @body_format = 'HTML';
  </code></pre>
  
  <p>That SQL works, but I need to get the image saved as a file first. If I can get the image directly on the email without saving it as a file, that's fine, but it needs to be embedded on the email and I only want to use SQL.</p>