Kasanova
Kasanova

Reputation: 593

How to add 1 day to current date and have result in format yyyymmdd in SQL Server?

I need to add 1 day to the current date and have the output in the format yyyymmdd. The code needs to be written on a stored procedure on the sql server.

currently my code is as follows:

 DECLARE @dat DATE

    select @dat = dateadd(DD, 1, getdate())
    SELECT @dat =LEFT(CONVERT(VARCHAR(8), @dat, 112),10) 

However, it seems like im doing something wrong as my output on the sql table is in the format yyyy-mm-dd. I need to get rid of the hyphens.

any suggestions guys? thanks in advance.

Upvotes: 1

Views: 19495

Answers (4)

Kasanova
Kasanova

Reputation: 593

select @dat = dateadd(DD, 1, getdate())
DECLARE @datCus varchar(8)
select @datCus=LEFT(CONVERT(VARCHAR(8), @dat, 112),10) 

The problem was that i assigned @dat to the insert statements values. However having a varchar variable to handle the converting part solved the problem (in this case @datCus).

Upvotes: 0

Devart
Devart

Reputation: 121902

Try this one -

DECLARE @date VARCHAR(8)
SELECT @date = CONVERT(VARCHAR(8), DATEADD(DAY, 1, GETDATE()), 112) 

SELECT @date

Upvotes: 0

LameCoder
LameCoder

Reputation: 1297

The issue is that you are assigning it back to a date object. You need to assign it to a varchar.

I did the following in SQL Server 2005:

DECLARE @dat DATETIME
DECLARE @string varchar(8)
SET @dat = GetUtcDate()

    select @dat = dateadd(DD, 1, getdate())

    SELECT @string =CONVERT(VARCHAR(8), @dat, 112) 
PRINT @string

Upvotes: 4

Declan_K
Declan_K

Reputation: 6826

Change the declaration of @dat to a STRING

Upvotes: 1

Related Questions