Passing Date as a parameter from package fails in string conversion

More
10 months 1 week ago - 10 months 1 week ago #25657 by prashant
 
hellos,

i have a sql query for a report which works fine with current date but I want to instead ask the user for a date instead 
DECLARE @start DATE = CONVERT(date, GETDATE());
--
 Using User Input I want to ask the user for a date and pass the same in ETL report , I did a set variable for #dd# to '2025-09-17' 
I modified the script to 

 DECLARE @start_string VARCHAR(10) = '#dd#';      
DECLARE @start DATE = CONVERT(DATE, @start_string,23);

But I keep getting Conversion failed when converting date and/or time from character string

When I do TRY_CONVERT I simply get a blank report  
Last edit: 10 months 1 week ago by prashant.
The following user(s) said Thank You: admin

Please Log in or Create an account to join the conversation.

More
10 months 1 week ago #25658 by admin
Can't see anything wrong with it
You might have spaces at the end/beginning or some invisible characters.
I would use trim to remove it

Or perhaps '#dd#' is never replaced
or it is replaced with '2025-09-17' 

eg:
 
DECLARE @start_string VARCHAR(10) = ''2025-09-17'';      
select  @start_string

DECLARE @start_string VARCHAR(10) = '#dd#';      
select  @start_string

Mike
ETL Architect

Please Log in or Create an account to join the conversation.

More
10 months 1 week ago #25659 by prashant
wel #dd# absolultey has the value , since i tried the same with show message

I introduced a trim, but still conversion error

DECLARE @start_string VARCHAR(10) = trim('#dd#');      
DECLARE @start DATE = CONVERT(DATE, @start_string,23);


Any other way to troubleshoot this"
The following user(s) said Thank You: admin

Please Log in or Create an account to join the conversation.

More
10 months 1 week ago #25660 by admin
Please create a minimalistic package and attach it here 

Mike
ETL Architect

Please Log in or Create an account to join the conversation.

More
10 months 1 week ago #25661 by prashant
i did try a 

SQL to text package and 

when I tried
DECLARE @start_string VARCHAR(10) = '2025-09-17';

SELECT 
    @start_string                        AS OriginalString,
    CAST(@start_string AS DATE)          AS CastedDate,
    CONVERT(DATE, @start_string)         AS ConvertedDate,
    TRY_CONVERT(DATE, @start_string)     AS TryConvertedDate;

My output was 
"OriginalString","CastedDate","ConvertedDate","TryConvertedDate" "2025-09-17","2025-09-17 00:00:00.000","2025-09-17 00:00:00.000","2025-09-17 00:00:00.000"

When I tried
DECLARE @start_string VARCHAR(10) = '2025-09-17';

SELECT 
    @start_string AS OriginalString,
    FORMAT(CAST(@start_string AS DATE), 'yyyy-MM-dd') AS CastedDate,
    FORMAT(CONVERT(DATE, @start_string), 'yyyy-MM-dd') AS ConvertedDate,
    FORMAT(TRY_CONVERT(DATE, @start_string), 'yyyy-MM-dd') AS TryConvertedDate;

Output was fine without timestamp

BUT THE MOMENT i tried passing  #dd# I start getting errors


Not able to read the data: SQLExecDirect(FHSMT,PChar ('DECLARE @start_string VARCHAR(10) = '2025-09-31';
 '), SQL_NTS) Failed: 22007 [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string.

Please Log in or Create an account to join the conversation.

More
10 months 1 week ago #25662 by prashant
 

File Attachment:

File Name: ABCD.zip
File Size:1 KB

Please Log in or Create an account to join the conversation.

Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Read more
Marketing
Set of techniques which have for object the commercial strategy and in particular the market study.
Google
Accept
Decline
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline
Google Analytics
Accept
Decline
Functional
Tools used to give you more features when navigating on the website, this can include social sharing.
Advertisement
If you accept, the ads on the page will be adapted to your preferences.
Google Ad
Accept
Decline
Save