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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:39:40+00:00 2026-06-12T16:39:40+00:00

Below I have a snippet of code from a stored procedure I’m trying to

  • 0

Below I have a snippet of code from a stored procedure I’m trying to make dynamic. Meaning I want to run the EXEC('select * from table_name WHERE filter=something') to return a result set to my web service. The week_start_date field in my table that I’m querying is a datetime field, not a date field. How do I properly append the datetime parameter to the dynamic SQL so it interprets it correctly in the query? Also, if you have suggestions on how to make the code cleaner, I’ll take advice.

When I look at the data, I have values that look like this:

2012-08-10 00:00:00.000

T-SQL code that is not working:

ALTER PROCEDURE [dbo].[logged_time_by_week]
    @week_start_date_filter datetime = null
AS
BEGIN

….

    DECLARE @select_clause nvarchar(4000);
    DECLARE @from_clause nvarchar(4000);
    DECLARE @where_clause nvarchar(4000);
    DECLARE @order_clause nvarchar(4000);

    SET @select_clause = 
        ' SELECT '
        + ' [sunday_hours],'
        + ' [monday_hours],'
        + ' [tuesday_hours],'
        + ' [wednesday_hours],'
        + ' [thursday_hours],'
        + ' [friday_hours],'
        + ' [saturday_hours]'

    SET @from_clause =
        ' FROM '
        + ' [timesheet_row] '

    SET @where_clause = ''

    IF @week_start_date_filter IS NOT NULL AND @week_start_date_filter != ''
    BEGIN 
        IF @where_clause != ''
        BEGIN
            SET @where_clause = @where_clause + ' AND ' 
        END
        SET @where_clause = @where_clause + ' [week_start_date] = ' + CONVERT(nvarchar, @week_start_date_filter)
    END

    IF @where_clause != '' 
    BEGIN
        SET @where_clause = ' WHERE ' + @where_clause
    END

    SET @order_clause = ' ORDER BY [' + @sort_column + '] ' + @sort_direction

    EXEC(@select_clause + @from_clause + @where_clause + @order_clause)
  • 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-12T16:39:41+00:00Added an answer on June 12, 2026 at 4:39 pm

    Easiest way to troubleshoot these kinds of errors is to print out the generated sql to see why it’s failing.

    Just by looking at your query i believe you just need to fix the quotes around the @week_start_date_filter variable.

    declare @week_start_date_filter datetime = getdate();
    print ' [week_start_date] = ''' + CONVERT(nvarchar, @week_start_date_filter) + '''';
    

    Also as a side note, I’d recommend instead to use parameterized sql if you have to use dynamic sql. Here’s an example using sys.objects since i don’t know the schema of your table.

    DECLARE
        @week_start_date_filter datetime    = (SELECT TOP(1) [create_date] FROM sys.objects),
        @sort_column            sysname     = N'create_date',
        @sort_direction         nvarchar(3) = N'ASC';
    
    
        DECLARE @select_clause nvarchar(4000);
        DECLARE @from_clause nvarchar(4000);
        DECLARE @where_clause nvarchar(4000);
        DECLARE @order_clause nvarchar(4000);
    
        SET @select_clause = 
            ' SELECT TOP(1) '
            + ' *';
    
        SET @from_clause =
            ' FROM '
            + ' sys.objects'
    
        SET @where_clause = ''
    
        IF @week_start_date_filter IS NOT NULL AND @week_start_date_filter != ''
        BEGIN 
            IF @where_clause != ''
            BEGIN
                SET @where_clause = @where_clause + ' AND ' 
            END
            SET @where_clause = @where_clause + ' [create_date] = @date_filter'
        END
    
        IF @where_clause != '' 
        BEGIN
            SET @where_clause = ' WHERE ' + @where_clause
        END
    
        SET @order_clause = ' ORDER BY [' + @sort_column + '] ' + @sort_direction
    
        DECLARE
            @SQL    nvarchar(max) = @select_clause + @from_clause + @where_clause + @order_clause,
            @Params nvarchar(200) = N'@date_filter datetime';
    
    
        EXEC sp_executesql
            @stmt        = @SQL,
            @params      = @Params,
            @date_filter = @week_start_date_filter;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this snippet of code below. I want to pass the value of
I have code similar to the snippet below (taken from http://leastprivilege.com/2009/05/24/use-geneva-session-management-for-your-own-needs/ ) public class
I have below snippet of code in which TestClass is extending jPanel which is
I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
Below is the snippet of my code. I have a JTable. I have extended
I have the below contents from a .txt file stored into a string variable
I have the following code snippet, which loads data from a CSV file into
I have been pulling my hair above below snippet of code - which is
i have below code snippet. I am not getting how to change the color

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.