Solved

Convert datetime to nvarchar in dynamic SQL statement

Posted on 2014-10-21
6
704 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

822 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