Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:20:00+00:00 2026-06-09T14:20:00+00:00

I’m stuck on the following sql server: DECLARE @sql AS NVARCHAR(500),@db as varchar(50),@value AS

  • 0

I’m stuck on the following sql server:

DECLARE @sql AS NVARCHAR(500),@db as varchar(50),@value AS CHAR(129);
SET @db = 'SSCT1';
SET @value = '1806-11-801-1000';
SET @sql = 'SELECT ACTINDX FROM ' + quotename(@db) 
  + '.[dbo].[GL00105] WHERE ACTNUMST = ' + @value;
EXEC (@sql);

When I run this in sql server I get :
Conversion failed when converting the varchar value ‘1806-11-801-1000

I checked the field I’m using the where clause against and it matches the type in the declaration (char(129) so I’m not sure what it’s trying to convert.

I’m trying to build a sql statement that will accept the db name as a variable in addition to the value. any thoughts?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:20:03+00:00Added an answer on June 9, 2026 at 2:20 pm

    I’m going to guess that ACTNUMST is a string column, in which case you need to delimit it correctly:

    SET @sql = 'SELECT ACTINDX FROM ' + quotename(@db) 
      + '.[dbo].[GL00105] WHERE ACTNUMST = ''' 
      + @value + ''';';
    

    If @value might ever contain apostrophes, you need to deal with that further:

    SET @sql = 'SELECT ACTINDX FROM ' + quotename(@db) 
      + '.[dbo].[GL00105] WHERE ACTNUMST = ''' 
      + REPLACE(@value, '''', '''''') + ''';';
    

    Yuck. A much safer approach is:

    DECLARE 
      @sql   NVARCHAR(MAX),
      @db    SYSNAME,
      @value CHAR(129);
    
    SELECT
      @db = N'SSCT1',
      @value = '1806-11-801-1000';
    
    SET @sql = N'SELECT ACTINDX FROM ' + quotename(@db) 
      + '.[dbo].[GL00105] WHERE ACTNUMST = @val;';
    
    EXEC sp_executesql @sql, N'@val CHAR(129)', @value;
    

    This guards a little better against dynamic SQL and also prevents you from having to deal with the quotes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm parsing an XML file, the creators of it stuck in a bunch social
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.