Solved

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

Posted on 2008-10-30
8
169 Views
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
0
Comment
Question by:cjhhiv
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
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)
0
 
LVL 8

Expert Comment

by:rpkhare
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.
0
 
LVL 74

Expert Comment

by:sdstuber
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())
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 125 total points
ID: 22843174
Optionally

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

will yield yyyymmdd


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

will yield yyyymmdd01
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
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.
0
 

Author Closing Comment

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

Author Comment

by:cjhhiv
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.

Thanks.
0
 
LVL 74

Expert Comment

by:sdstuber
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
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

839 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