Solved

Need to insert text into a IMAGE field as Double Byte from vbscript into SQL table

Posted on 2016-11-04
10
73 Views
Last Modified: 2016-11-09
I had a vbscript (macro) that inserted records into a IMAGE field in a SQL Server database,

              pmspec = pmspec & vbCrLf & "Reason for Obsoletion: " & result & " By: " & Network_User & " - " & Now()
              Set db = CreateObject("ADODB.Connection")
              db.Open SCONNUPD
              SET RSu = CREATEOBJECT("ADODB.RECORDSET")
              SSQLUPD = "INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('" & ID & "'" & chr(44) & "'D'" & chr(44) & "'" & pmspec & "'" & chr(44) & Len(pmspec) & ")"
              Set RSu = db.Execute(SSQLUPD)

Open in new window

Now our ERP vendor has upgraded all of their fields to DoubleByte (including the BITS (IMAGE) field above.

I can not figure out how to write the insert now to convert my text to DoubleByte.

I do know that I will need to change my Len(pmspec) to LenB(pmspec)  (the table stores the length in bytes of the field into BITS_LENGTH)

Any help is greatly appreciated.
0
Comment
Question by:BFanguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
10 Comments
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 41874722
put in N in front of the BITS value.

something like this:

              pmspec = pmspec & vbCrLf & "Reason for Obsoletion: " & result & " By: " & Network_User & " - " & Now()
              Set db = CreateObject("ADODB.Connection")
              db.Open SCONNUPD
              SET RSu = CREATEOBJECT("ADODB.RECORDSET")
              SSQLUPD = "INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('" & ID & "'" & chr(44) & "'D'" & chr(44) & " N'" & pmspec & "'" & chr(44) & Len(pmspec) & ")"
              Set RSu = db.Execute(SSQLUPD)

Open in new window

0
 

Author Comment

by:BFanguy
ID: 41874737
odbc sql server driver Operand type clash: nvarchar is incompatible with image
0
 

Author Comment

by:BFanguy
ID: 41874738
i am guessing i have to convert it to unicode double byte before i try to insert???
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:BFanguy
ID: 41874745
tried from ssms:

INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D', N'test', Len('test')*2)

Msg 206, Level 16, State 2, Line 1
Operand type clash: nvarchar is incompatible with image
0
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 250 total points
ID: 41874850
ok, data type in the database is image.

https://msdn.microsoft.com/en-us/library/ms187993.aspx

Then do a conversion cast as image

https://msdn.microsoft.com/en-us/library/ms191530.aspx

INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D', cast('test' as image), Len('test')*2)

Open in new window

0
 

Author Comment

by:BFanguy
ID: 41877028
thanks, tried your statement, the app show chinese characters.

tried
INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D', cast( N'test' as image), Len('test')*2)

Open in new window


Explicit conversion from data type nvarchar to image is not allowed.
0
 

Accepted Solution

by:
BFanguy earned 250 total points
ID: 41877040
Tried this and it worked.  thanks.

INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D',CAST(CAST('test' AS NVARCHAR(MAX)) AS VARBINARY(MAX)), Len('test') * 2 )

Open in new window

0
 

Author Comment

by:BFanguy
ID: 41877455
Jeff, sorry i thought i could give you the points even if i picked best solution.  trying to figure out what i have to do to give you the points
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
index related performance counter  for SQL server DB 11 37
WMIC Output Formatting 1 84
grouping by date only 6 22
msiexec won't run 4 24
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

739 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