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 6676909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:03:04+00:00 2026-05-26T04:03:04+00:00

Using Sql Server 2008, developed a view vw_MasterView I get this error: Conversion failed

  • 0

Using Sql Server 2008, developed a view “vw_MasterView” I get this error:

Conversion failed when converting the nvarchar value 'SELECT * FROM VW_MASTERVIEW v WHERE 1=1  AND v.ClinicId = '' to data type int.

when I run the following stored procedure:

USE [xxxxxxx]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ClientSummary1]
    @LocationId int,
    @startDate datetime,
    @endDate datetime,
    @userName nvarchar(50)
AS
    declare @sql nvarchar(2000);
    SET NOCOUNT ON
    set @sql = 'SELECT * FROM VW_MASTERVIEW v WHERE 1=1 ';
    IF @LocationId is not null
    begin
        set @sql = @sql + ' AND v.LocationId = ''' + @LocationId + '''';
    end
    if @userName is not null
    begin
        set @sql = @sql + ' AND (v.FirstUser = '' OR v.SecondUser = '')' + @userName + '''';
    end
    if @startDate is not null 
    begin
       set @sql = @sql + ' AND v.FirstVisitDate = ''' + @startDate + ''''; 
    end
    if @endDate is not null 
    begin
        set @sql = @sql + ' AND v.LastVisitDate = ''' + @endDate + ''''; 
    end

    EXEC(@sql)

I get both the LocationId and userName from a VS2010 application.
Thanks in Advance

  • 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-05-26T04:03:05+00:00Added an answer on May 26, 2026 at 4:03 am

    When appending strings together in SQL Server, you have to cast non-textual types (such as int) to a textual type (such as varchar):

    set @sql = @sql + ' AND v.LocationId = ''' +
        cast(@LocationId as varchar(10)) + '''';
    --  ^^^^ have to cast           ^^ make sure size is big enough
    

    Note that dynamic SQL should not be necessary in the first place. You can just run the query directly with the parameters (I implemented the null checks with the extra or conditions):

    SELECT * FROM VW_MASTERVIEW v
     WHERE (v.LocationId = @LocationId    OR @LocationId is null)
       AND (v.FirstUser = @userName OR v.Seconduser = @userName OR @userName is null)
       AND (v.FirstVisitDate = @startDate OR @startDate is null)
       AND (v.LastVisitDate = @endDate    OR @endDate is null)
    

    I may not have the logic right for FirstUser and SecondUser – I took an educated guess from your incomplete code.

    Hope this helps!

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

Sidebar

Related Questions

I have developed an application using VB.NET, Visual Studio 2008 and the SQL Server
I built this app with Web Developer Express 2010 using SQL Server Express 2008.
I can connect to my sql server 2008 developer server using this in a
We are using SQL Server 2008 R2 Standard and linked servers to get information
I am using SQL Server 2008 developer edition. I was trying to attach the
I have a database that I have created using SQL Server Developer 2008. I
I've just installed SQL Server 2008 Developer edition and I'm trying to connect using
Using SQL Server 2008, is there a way to allow inserts to a table
I'm using SQL Server 2008 with Advanced Services on my Vista Home Premium. I'd
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just

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.