INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Converting Text to Date and Using in a Parameter

Converting Text to Date and Using in a Parameter

(OP)
SQL Server 2008 R2

I have a stored procedure (simplified) for a SSRS report. The report runs fine but I'm getting an error using a 3rd party product relating to converting parameter value from a String to DateTime.

ALTER PROCEDURE [dbo].[usrsp_S_Activity]
@Start Date,
@End Date,
AS
SELECT
CLIENT.NAME
,CLIENT.CLIENT_ID
,ACT_LOG.CREATE_DATE
,CLIENT.ACTIVE_IND
,Convert(Date,ACT_LOG.CREATE_DATE)AS 'DT_CREATE'

FROM
[CSPRDWEBSRV].[ACTIVITY].[dbo].[CLIENT] CLIENT
INNER JOIN
[CSPRDWEBSRV].[ACTIVITY].[dbo].[ACT_LOG] ACT_LOG ON CLIENT.CLIENT_ID = ACT_LOG.CLIENT_ID

WHERE
ACT_LOG.CREATE_DATE BETWEEN (@Start) and (@End)

The problem I am having is a result the "CREATE_DATE" being stored in the database as varchar(50)and this unfortunately cannot be changed. In the database, the field is formatted 'm/d/yyyy' (sample values: 1/1/2017 and 2/28/2017).

Is there a better way to write the stored procedure format the date?

(I also posted this to SSRS but that forum does not get any action.)

Thanks so much!

- Jackie

RE: Converting Text to Date and Using in a Parameter

Can you check the values in the table to see if there are any invalid values?

CODE

Select * 
From   ACT_LOG
Where  IsDate(CREATE_DATE) = 0 

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Converting Text to Date and Using in a Parameter

(OP)
No invalid dates.

Thanks for responding.

RE: Converting Text to Date and Using in a Parameter

CODE

WHERE CONVERT(DATE, ACT_LOG.CREATE_DATE, 101) BETWEEN @Start and @End 

RE: Converting Text to Date and Using in a Parameter

(OP)
I did try that and the date looked formatted correctly 'yyyy-mm-dd'

I was suspicious when trying to execute the stored procedure and got error after inputting 2017-01-01 and 2017-02-28:

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '-'.

--------
USE [SQLReports]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[usrsp_S_Activity]
@Start = 2017-01-01,
@End = 2017-02-15

SELECT 'Return Value' = @return_value

GO
---------
I am at a loss :(

RE: Converting Text to Date and Using in a Parameter

You should put apostrophes around your dates when calling the stored procedure. Like this:

CODE

DECLARE	@return_value int

EXEC	@return_value = [dbo].[usrsp_S_Activity]
@Start = '2017-01-01',
@End = '2017-02-15'

SELECT	'Return Value' = @return_value 

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: Converting Text to Date and Using in a Parameter

(OP)
Yes, thanks that worked. I just wanted to verify the syntax before approaching the vendor.

- Jackie

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close