Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server convert day into date format

Posted on 2014-07-31
7
Medium Priority
?
334 Views
Last Modified: 2014-07-31
Hello Experts Exchange
I have a field in SQL Server 2012 that just has the day of the month in it, I need to convert the day into a full date.

For example the field would have 31 and I want it to become 31/07/2014.

Is there a SQL script that can do this?

Thank you for reading.

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40231178
the question to ask is if you have a value of 31, and are in the month of Feb, April etc where there is no such day: what should happen?
0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40231181
Hi,

My friend if the field contains 31 then there are total 8 months which has 31 Days so what should be the logic to pick which month to display for 31 .
Also have to set the same for 30, 28 , 29 (Feb in Leap year)
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40231185
a simple started (without looking at the above question) would be this:;
declare @dayofmonth int
set @dayofmonth = 31

select dateadd(day, @dayofmonth - datepart(day, getdate()), cast(getdate() as date))

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 66

Expert Comment

by:Jim Horn
ID: 40231458
>For example the field would have 31 and I want it to become 31/07/2014.
What's the logic for the 7 and 2014? If this is the current month/year, then SQL 2012 introduces EOMONTH, which returns the last day of the month for any given date. EOMONTH
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40231531
Turing only 31, without month and year, into a date will be an utter guess
Presumably there are values of 1 through 31, and turning any of those would be an utter guess too.

I would ask if you know of some inherent logic behind this curious field?

Is there anything else in a row to give us a clue on  month and year?
(perhaps you could provide the column headings and a few rows of this data to look at?)
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40231722
--code to get "day_number" day of the current month

SELECT DATEADD(DAY, day_number - 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
FROM (
    SELECT 31 AS day_number UNION ALL
    SELECT 17
) AS test_data
0
 

Author Closing Comment

by:SQLSearcher
ID: 40231927
Thank you for your help.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

782 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