Solved

Help Convert to a Date and Back

Posted on 2008-06-20
7
243 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

770 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