Solved

Help Convert to a Date and Back

Posted on 2008-06-20
7
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

624 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