Saturday, September 28, 2019

How to store image in SQL Server database table column


Below are the steps to import image file to sql server database

Step 1:

Create table

CREATE TABLE ImageUpload (
   ImageName NVARCHAR(40) PRIMARY KEY NOT NULL
   , ImgFileName NVARCHAR (100)
   , DataFile VARBINARY (max)
   )
GO

Step 2:

Insert record

insert into ImageUpload (ImageName, ImgFileName, DataFile)
select 'NewYearCake','Newyear.jpg',BulkColumn
FROM Openrowset( Bulk 'G:\Image\DSC02319.JPG', Single_Blob) as img


select * from ImageUpload



You can verify whether image got uploaded correctly by using SSRS.
Please visit the below link for more info.


Display Database Images in SSRS Report

SQL Server - Different types Formatting of Date/Time with Example


MM/DD/YYYY to YYYY/MM/DD

Declare @DateFormat varchar(20)
set @DateFormat='02/21/2019' --MM/DD/YYYY
Select @DateFormat as SourceFormat, CONVERT(Date,@DateFormat,101) as TargetFormat

SourceFormat   TargetFormat
02/21/2019         2019-02-21

DD/MM/YYYY to YYYY/MM/DD

Declare @DateFormat varchar(20)
set @DateFormat='24/02/2019' --DD/MM/YYYY
Select @DateFormat as SourceFormat, CONVERT(Date,@DateFormat,103) as TargetFormat

SourceFormat   TargetFormat
24/02/2019         2019-02-24

YYYY/MM/DD to DD/MM/YYYY

Declare @DateFormat varchar(20)
set @DateFormat='2019/05/24' --YYYY/MM/DD
Select @DateFormat as SourceFormat, CONVERT(VARCHAR(10), CAST(@DateFormat AS DATETIME), 103) as TargetFormat

SourceFormat   TargetFormat
2019/05/24         24/05/2019


DD/MM/YYYY to MM/DD/YYYY

Declare @DateFormat varchar(20)
set @DateFormat='20/05/2019' --DD/MM/YYYY
Select @DateFormat as SourceFormat, convert(varchar, convert(date, @DateFormat, 105), 101) as TargetFormat

SourceFormat   TargetFormat
20/05/2019         05/20/2019

MM-DD-YYYY to DD-MM-YYYY

Declare @DateFormat varchar(20)
set @DateFormat='05-24-2019' --MM-DD-YYYY
Select @DateFormat as SourceFormat, convert(varchar, convert(date, @DateFormat, 101), 105) as TargetFormat

SourceFormat   TargetFormat
05-24-2019          24-05-2019