How do I enter the date into a column in a specific way without having them add up?

Posted on 2008-10-30
Last Modified: 2012-05-05
I'm trying to insert a date plus "01"  into my database in a very specific format. For example, if I were inserting this today I would insert 2008103001. I've tried the code below, but it simply adds my values together and would put 2050 into the field instead what I want. I'm sure it's something simple, but I'm too stupid to figure it out.

UPDATE    table
SET thisfield = YEAR(GETDATE()) + MONTH(GETDATE()) + DAY(GETDATE()) + 01
Question by:cjhhiv
  • 3
  • 2
  • 2
  • +1
LVL 39

Expert Comment

ID: 22843153
The below query will return TOMORROW in a DATE only format with NO TIME.

select convert(datetime, convert(varchar(10), getdate()+1, 101), 101)

Expert Comment

ID: 22843155
What is the Datatype of the column in which you are inserting this value? I guess DateTime will not accept what you are desiring. You need to take Varchar.
LVL 73

Expert Comment

ID: 22843159
what do you mean by a date plus "01"

"01" is a string,  a date is a date, it doesn't make sense to add them together.

Do you mean add one hour to a date?

then do something like this...

update yourtable set thisfield = dateadd(hh,1,GetDate())
LVL 39

Accepted Solution

BrandonGalderisi earned 125 total points
ID: 22843174

select convert(char(8), getdate(),112)

will yield yyyymmdd

select convert(varchar(10), getdate(),112)  + '01'

will yield yyyymmdd01
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

LVL 39

Expert Comment

ID: 22843181
You REALLY don't want to do this though.  You should store dates as dates and handle formatting when it's time to display it.

Author Closing Comment

ID: 31511767
Thanks "select convert(varchar(10), getdate(),112)  + '01'" was exactly what I was looking for!

Author Comment

ID: 22843225
Thanks all. I should have clarified, which BrandonGalderisi picked up on. I wanted to insert a number into the field that was based on a date. The number needed to have '01' added to the end. It was an nvarchar(50) field.

LVL 73

Expert Comment

ID: 22843270
I will second BrandonGalderisi's warning.  Store dates as dates,  it removes all ambiguity and makes operations more efficient and less error prone.

If it's at all feasible to do so,  modify that table so it's datatypes are correct

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now