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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:31:40+00:00 2026-06-09T08:31:40+00:00

I have a big procedure which consists mostly dynamic SQL. I am having issues

  • 0

I have a big procedure which consists mostly dynamic SQL. I am having issues with setting one of the date fields.

DECLARE @WorkDate DATETIME 

SET @WorkDate = 'SELECT MIN(__Insert_Date) FROM  ' + @DatabaseName + '.'
                            + @SchemaName + '.' + @TableName + '_Hist'

SET @WorkDate = DATEADD(DAY, DATEDIFF(DAY, '19000101', @WorkDate), '19000101')

This is part of a big procedure. So when I execute the above query I am getting this error:

Msg 241, Level 16, State 1, Line 68
Conversion failed when converting date and/or time from character string.

  • 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-09T08:31:40+00:00Added an answer on June 9, 2026 at 8:31 am

    @WorkDate is DATETIME and your setting it at as a sting thats why the conversion is failing
    EDIT :

    Try this:

    DECLARE @WorkDate DATETIME, @WorkDateString varchar(100)
    
    SET @WorkDateString = 'SELECT MIN(__Insert_Date) FROM  ' + @DatabaseName + '.'
                            + @SchemaName + '.' + @TableName + '_Hist'
    
    SET @WorkDate = DATEADD(DAY, DATEDIFF(DAY, '19000101', @WorkDate), '19000101')
    

    If you need to insert the @WorkDate into the select string where __Insert_Date is then you need to reverse the order to look like this

    DECLARE @WorkDate DATETIME, @WorkDateString varchar(100)
    
    SET @WorkDate = DATEADD(DAY, DATEDIFF(DAY, '19000101', @WorkDate), '19000101')
    
    SET @WorkDateString = 'SELECT MIN(' + CAST(@WorkDate as varchar(19)) + ') FROM  ' + @DatabaseName + '.'
                            + @SchemaName + '.' + @TableName + '_Hist'
    

    Don’t know if you looking to insert the @WorkDate into the select string but that’s how you could accomplish that

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

Sidebar

Related Questions

I have big URL list, which I have to download in parallel and check
I have big issues in tracing down 'bitmap size exceeds VM budget' error. I
I have big dataset (but the following is small one for example). I can
I have a big stored procedure that I run. At the end its supposed
Circumstances I have a procedure which will construct a matrix using the given list
Here's my scenario: Let's say I have a stored procedure in which I need
I have a SQL Server 2008 database which contains data that I need to
I have to parse a big bunch of log files, which are in the
I have to do a big update script - not an SPL (stored procedure).
i have a stored procedure that takes 2 parameters: ID and Date. when i

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.