Solved

Convert datetime to nvarchar in dynamic SQL statement

Posted on 2014-10-21
6
728 Views
Last Modified: 2014-10-21
I'm migrating some queries from Access to SQL Server stored procedures.

Have one SP that is working great right now, but which requires me to build the SQL string dynamically.  I'm trying to add a criteria to the string checks whether a  date field is null or is less than a date that is passed into the procedure, but I am getting an error:

The data types nvarchar and datetime2 are incompatible in the add operator.

The line that is causing the problem is:

WHERE ((WF.Eff_Date IS NULL) OR (WF.Eff_Date <' + dateadd(day, 1, @EndDate) + '))

There are lines preceding and following this text which is why there is no opening or closing ' on this particular line.  What I want this to look like when the code executes is:

WHERE ((WF.Eff_Date IS NULL) OR (WF.Eff_Date < '1014-10-22'))

Where the @EndDate value passed into the stored procedure is '2014-10-21'

I tried using & to set off the dateadd function, but that didn't work either.
0
Comment
Question by:Dale Fye (Access MVP)
6 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40394898
Shouldn't it just read:

WHERE ((WF.Eff_Date IS NULL) OR (WF.Eff_Date < dateadd(day, 1, @EndDate) ))

as DateAdd returns a date value.

/gustav
0
 
LVL 47

Author Comment

by:Dale Fye (Access MVP)
ID: 40394916
Gustav,

When I left all of that code inside the single quotes that define the string, it gave me an error with the @EndDate parameter. Cannot remember exactly what the message was, but I assumed that the exec line could not properly execute the dynamic string because @EndDate is not defined within the dynamic SQL.

Dale
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40394933
Hmm. I thought @EndDate was defined as a parameter.
Not quite sure what you are doing.

/gustav
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 100 total points
ID: 40394945
>requires me to build the SQL string dynamically.
>WHERE ((WF.Eff_Date IS NULL) OR (WF.Eff_Date <' + dateadd(day, 1, @EndDate) + '))
Since you're using Dynamic SQL, when concatenting a string that also uses non-string values such as dates or numbers you'll need to CAST any other data type (such as date) to a char value

WHERE ((WF.Eff_Date IS NULL) OR (WF.Eff_Date <' + CAST(dateadd(day, 1, @EndDate) as varchar(25)) + '))

Open in new window

0
 
LVL 18

Accepted Solution

by:
lludden earned 400 total points
ID: 40395220
You need to convert the date to text, then put that in single quotes.  

@SQL = 'WHERE ((WF.Eff_Date IS NULL) OR (WF.Eff_Date <''' + CONVERT(varchar(10),dateadd(day, 1, @EndDate),110) + '''))'

Open in new window

0
 
LVL 47

Author Closing Comment

by:Dale Fye (Access MVP)
ID: 40395719
Thanks, guys.

I ended up using Cast, but then still had to add in the additional single quotes.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question