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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:55:31+00:00 2026-06-04T20:55:31+00:00

This executes correctly: (It is weird that I needed to use ” by the

  • 0

This executes correctly: (It is weird that I needed to use ” by the date for it to actually execute)

DECLARE 
@cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname) FROM [85137_PHY_Long_PG] c FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

set @query = 'SELECT statdate, ' + @cols + ' from 
        (
            select statdate, statcolumnname, statcolumnvalue
            from [85137_PHY_Long_PG]
       ) x
        pivot 
        (
             min(statcolumnvalue)
            for statcolumnname in (' + @cols + ')
        ) p WHERE statdate BETWEEN ''2012-04-01 12:15:00'' AND ''2012-04-01 12:45:00''      ORDER BY statdate'

execute(@query)

Now I want to replace the dates with variables:

DECLARE 
@cols AS NVARCHAR(MAX),
@query  AS NVARCHAR(MAX),
@from  AS NVARCHAR(MAX),
@to  AS NVARCHAR(MAX);

set @from = '2012-04-01 12:15:00'
set @to = '2012-04-01 12:45:00'

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname) FROM [85137_PHY_Long_PG] c FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')

set @query = 'SELECT statdate, ' + @cols + ' from 
        (
            select statdate, statcolumnname, statcolumnvalue
            from [85137_PHY_Long_PG]
       ) x
        pivot 
        (
             min(statcolumnvalue)
            for statcolumnname in (' + @cols + ')
        ) p WHERE statdate BETWEEN ''+@from+'' AND ''+@to+'' ORDER BY statdate'

execute(@query)

I get the following error:Conversion failed when converting character string to smalldatetime data type

Changing the where statement to the following:

WHERE statdate BETWEEN ''+convert(smalldatetime,@from)+'' AND ''+convert(smalldatetime,@to)+'' ORDER BY statdate'

Still gives me the same error, just can’t seem to replace the dates as variables

  • 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-04T20:55:33+00:00Added an answer on June 4, 2026 at 8:55 pm

    ” is not weird; it is a notation that enables apostrophes inside varchars.

    When concatenating make sure that you are not trying to concatenate anything other than (n)varchars and (n)chars because Sql Server will attempt to convert them to other datatypes; in your case, in smalldatetime. You might avoid this trouble by explicitly converting your parameter dates to nvarchars before/during concatenation, but better solution is to use sp_executesql and parameters.

    If you leave parameters inside query:

    set @query = 'SELECT statdate, ' + @cols + ' from 
        (
            select statdate, statcolumnname, statcolumnvalue
            from [85137_PHY_Long_PG]
       ) x
        pivot 
        (
             min(statcolumnvalue)
            for statcolumnname in (' + @cols + ')
        ) p WHERE statdate BETWEEN @from AND @to ORDER BY statdate'
    

    You can execute it with parameters:

    exec sp_executesql @query, N'@from datetime, @to datetime', @from=@from_variable, @to=@to_variable
    

    Where @from_variable and @to_variable are datetime variables defined earlier in batch.

    UPDATE:

    If your ultimate goal is to wrap this code in stored procedure, here is a template:

    create proc MyProc (@dateFrom smalldatetime, @dateTo smalldatetime)
    as
    DECLARE 
    @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.statcolumnname) 
      FROM [85137_PHY_Long_PG] c 
       FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
    set @query = 'SELECT statdate, ' + @cols + ' from 
        (
            select statdate, statcolumnname, statcolumnvalue
            from [85137_PHY_Long_PG]
       ) x
        pivot 
        (
             min(statcolumnvalue)
            for statcolumnname in (' + @cols + ')
        ) p WHERE statdate BETWEEN @from AND @to ORDER BY statdate'
    
    exec sp_executesql @query, N'@from smalldatetime, @to smalldatetime', @from=@dateFrom, @to=@dateTo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure if I can explain this correctly, but I am trying to execute
I have this procedure that executes another procedure passed by a parameter and its
I have a ruby application that executes ant as a subprocess using backtick. This
I have a script task that executes a dynamically created SQL restore statement. This
I have this weird issue. I am trying to create a service that synchs
I have the following ajax call in my javascript. This call executes correctly, the
Weird... Originally i thought that php was not correctly handling large file uploads (800mb-2gb)
I'm currently struggling with this Collection was modified; enumeration operation may not execute issue.
I'm using NtQueryObject(handle, OBJECT_INFORMATION_CLASS.ObjectTypeInformation, IntPtr.Zero, 0, out length); in my program, but this executes
This code executes handbrakecli (a command line application) and places the output into a

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.