Solved

Help Convert to a Date and Back

Posted on 2008-06-20
7
240 Views
Last Modified: 2010-04-21
Hi All,
I have an Stored Procedure.  I want to get the current month end from a string and return the value as a string '20080429'

So I pass in string '20080429'
and it returns a string '20080430'
This is want I've written but it doesn't work....

CREATE PROCEDURE Caoimhe @value_dt varchar(8)
as
declare @current_monthend varchar(8)
declare @monthend varchar(8)
select @monthend = dateadd(day, -1,dateadd(month, 1,convert(datetime,left(@value_dt,6) + '01',112)))
select @current_monthend = convert(varchar(8),@monthend,112)
return @current_monthend
0
Comment
Question by:MickeyMin
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21830185
compute the value of @MonthEnd like this


select @monthend = CONVERT( varchar(8), dateadd(day, -1,dateadd(month, 1,convert(datetime,left(@value_dt,6) + '01',112))), 112)
0
 

Author Comment

by:MickeyMin
ID: 21830211
I get this..

 22018(257)[DataDirect][ODBC Sybase Wire Protocol driver][SQL Server]Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed.  Use the CONVERT function to run this query.
 (0.17 secs)

I've changed the SP to this..

CREATE PROCEDURE Caoimhe @value_dt varchar(8)
as



declare @current_monthend varchar(8)
declare @monthend varchar(8)
select @monthend = CONVERT( varchar(8), dateadd(day, -1,dateadd(month, 1,convert(datetime,left(@value_dt,6) + '01',112))), 112)



return @current_monthend

thanks
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21830465
CREATE PROCEDURE Caoimhe @value_dt varchar(8)
as
declare @current_monthend varchar(8)
declare @monthend varchar(8)
select @monthend = CONVERT( varchar(8), dateadd(day, -1,dateadd(month, 1,convert(datetime,left(@value_dt,6) + '01',112))), 112)
return @monthend
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 21830476
CREATE PROCEDURE Caoimhe @value_dt varchar(8),@monthend varchar(8) out
as
select @monthend = CONVERT( varchar(8), dateadd(day, -1,dateadd(month, 1,convert(datetime,left(@value_dt,6) + '01',112))), 112)
GO

DECLARE @Monthend varchar(8)
EXEC Caoimhe '20070709', @Monthend OUT
SELECT @Monthend
0
 

Author Comment

by:MickeyMin
ID: 21830486
I'm sorry but I get the same error

Thanks
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 21830509
is it sybase ? or sql server ?
0
 

Author Closing Comment

by:MickeyMin
ID: 31469113
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now