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

  • SEARCH
  • Home
  • 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 8906293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:37:02+00:00 2026-06-15T02:37:02+00:00

I keep getting the following error: Msg 241, Level 16, State 1, Line 9

  • 0

I keep getting the following error:

Msg 241, Level 16, State 1, Line 9 Conversion failed when converting
datetime from character string.

Here is the code I am trying to execute:

  DECLARE @v_sql  varchar(max),
  @v_database varchar(25),
  @vStartTime     DATETIME,
  @vEndTime       DATETIME

SELECT @v_database = N'[DATABASE_NAME]', @vStartTime = '2012-09-27', @vEndTime = '2012-11-27'


SELECT @v_sql = N'SELECT
  (SELECT ID FROM DATASTORE.DBO.PLANT WHERE DESCRIPTION = ''Henderson''),
  SEQ, 
  AID, 
  NAME, 
  GRP, 
  AREA, 
  PRIO,
  CASE ITIME WHEN ''-'' THEN NULL ELSE CAST(SUBSTRING(ITIME,1,8) + '' '' + SUBSTRING(ITIME,9,2) + '':'' + SUBSTRING(ITIME,11,2) + '':'' + SUBSTRING(ITIME,13,2) AS DATETIME) END ITIME,
  CASE ATIME WHEN ''-'' THEN NULL ELSE CAST(SUBSTRING(ATIME,1,8) + '' '' + SUBSTRING(ATIME,9,2) + '':'' + SUBSTRING(ATIME,11,2) + '':'' + SUBSTRING(ATIME,13,2) AS DATETIME) END ATIME,
  CASE NTIME WHEN ''-'' THEN NULL ELSE CAST(SUBSTRING(NTIME,1,8) + '' '' + SUBSTRING(NTIME,9,2) + '':'' + SUBSTRING(NTIME,11,2) + '':'' + SUBSTRING(NTIME,13,2) AS DATETIME) END NTIME,
  DUR, 
  MSG,
  VAR1,
  VAR2,
  VAR3,
  VAR4, 
  OPR, 
  USER_COMMENT
FROM ' + @v_database + '.PROD.ALARM
WHERE CAST(substring(ITIME, 1, 4) + ''-'' + substring(ITIME, 5, 2) + ''-'' + substring(ITIME, 7, 2) + '' '' + substring(ITIME,9,2) + '':'' + substring(ITIME,11,2) + '':'' + substring(ITIME,13,2) + substring(ITIME,15,3) AS DATETIME) BETWEEN  ' + @vStartTime +' AND ' + @vEndTime + ' ORDER BY ITIME'
EXEC(@v_sql)

Any help would be much appreciated, I am looking into this for a co-worker and it’s got us both stumped.

Edit with a bit more digging, we were able to resolve it ourselves, passing parameters to sp_executesql:

declare
  @vSql     NVARCHAR(MAX),
  @vParam    NVARCHAR(MAX),
  @vDatabase       VARCHAR(15)

SET @vParam = '@vStartTime DATETIME, @vEndTime DATETIME'

 SELECT @vSql = '
    SELECT ''
      '+ @vDatabase + ''',
      ITEM_CODE,
      SOURCE,
      DEST,
      TRAN_DT,
      MILL_NAME,
      NULL
    FROM ' + @vDatabase + '.PROD.GRD_LOG
    WHERE TRAN_DT BETWEEN @vStartTime AND @vEndTime'

EXEC sp_executesql @vSql, @vParam, @vStartTime, @vEndTime

By making the variables NVARCHAR(MAX), and then using sp_executesql instead of just executing the @vSql variable, we were able to resolve our issue.

Thanks to anyone who might have been looking into this.

  • 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-15T02:37:03+00:00Added an answer on June 15, 2026 at 2:37 am

    In your original dynamic SQL, you were trying to add datetime variable to a text string which results in SQL attempting to convert the text string to a datetime value (by the order of precedence of conversion). You need to set the variables to nvarchar as well to avoid conversion in the original dynamic SQL.

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

Sidebar

Related Questions

I keep getting the following error when trying to build the masstransit source from
I keep getting the following error: Method not found: 'Void Castle.MicroKernel.ComponentActivator.ComponentActivatorException..ctor this is from
I keep getting the following error: Parse error: syntax error, unexpected T_SL in /home/a4999406/public_html/willingLog.html
I keep getting the following error: AttributeError: Caribou instance has no attribute 'on_key_up' The
I keep getting the following error message ERROR 1064 (42000): You have an error
I keep getting the following error when trying to run a login script I
When I deploy my application to GoogleAppEngine I keep getting the following error Uncaught
I'm trying to get http://www.gelens.org/code/gevent-websocket/ running and keep getting the following error. socket_id=1 already
I'm using Visual Studio 2008 Express edition, and keep getting the following error: Cascadedisplay.h(4)
I keep getting an error with the following bit of code. It is probably

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.