Solved

convert this back to a date?

Posted on 2016-09-16
13
58 Views
Last Modified: 2016-09-30
I want this result to be converted to a date.  The point of the below code was to return only month and year but now i want that to be recognized as a date .

SELECT CONVERT(VARCHAR, MONTH(getdate())-1, 120) + '/' + CONVERT(VARCHAR, YEAR(getdate())-1, 120)  

SO I was hoping to do something like this but i got an error

SELECT CONVERT(datetime, CONVERT(VARCHAR, MONTH(getdate()), 120) + '/' + CONVERT(VARCHAR, YEAR(getdate()), 120)  , 104)
0
Comment
Question by:vbnetcoder
  • 4
  • 4
  • 2
  • +1
13 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41801793
Try this


SELECT DATEFROMPARTS(YEAR(GETDATE()) , MONTH(getdate()) , '01' )
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41801797
Or this what ever you need ...

SELECT DATEFROMPARTS(YEAR(GETDATE()) , MONTH(getdate()) , DAY(GETDATE()) )

Open in new window

0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41801798
In this you easily do the maths also e.g. month - 1 etc.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41801816
>return only month and year
>but now i want that to be recognized as a date
The above two comments are mutually exclusive, as formatting a datetime into anything that only shows part of the whole value will convert it to a character value.

Would probably help to give is a 'before and after' data mockup of what you're trying to do here, and your overall requirements as we may be able to recommend a better workaround.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41801822
If you need mm/dd/yyyy format use below

--

SELECT CONVERT(VARCHAR(10), DATEFROMPARTS(YEAR(GETDATE()) , MONTH(getdate()) , DAY(GETDATE())) ,101)

--

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41801840
Don't convert to char at all, that just adds complexity and slows things down:

SELECT DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0))

The "standard best-practice" way to adjust the current date to a given time period is:

DATEADD(<time_period>, DATEDIFF(<time_period>, 0, GETDATE()), 0)

That code very flexibly and  efficiently "zeros out" the time and puts you to the start of the specified time period.  Some specific examples:

SELECT
    DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0),
    DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0),
    DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)

The code above simply adapts the MONTH adjustment to backup one month and then to subtract one year from that.
0
 

Author Comment

by:vbnetcoder
ID: 41801844
Jim - you answered my question. I figured that was the case but wasn't sure. I have a report were i am only interested in month/year so i was hoping there was something to make it be recognized as a year.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41801870
Ok.  

If all you're going to do is display the mm/yy value and never touch it, then varchar is fine.

If this column will be touch in ways such as passed parameters or column sorting where you expect the sort order to behave like a date, then another option is to pass BOTH the mm/yy varchar and datetime value, show the mm/yy column but hide the datetime column, and add logic to your report that when you sort the mm/yy column it actually sorts the set based on the datetime column.
1
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41801917
Do any/all conversions from date/datetime to character formats in the last query to process/present the data, since character format is less efficient for SQL to use.
1
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41823879
I object to Khowal's claimed solution because "DATEFROMPARTS" is not available in SQL 2008, as specified in the initial q.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 41823881
I object to Khowal's claimed solution because "DATEFROMPARTS" is not available in SQL 2008, as specified in the initial q.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Query Syntax Error 9 32
separate column 24 19
Trouble connecting to SqlServer database 4 31
Sql query for filter 12 21
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 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

13 Experts available now in Live!

Get 1:1 Help Now