?
Solved

Date format in sql

Posted on 2015-02-17
11
Medium Priority
?
460 Views
Last Modified: 2015-03-09
How can I return 2/01/2015 to below string format varchar (please) in sql?

2/01/2015 to 02/01/2015
2/01/2015 to get date only 01
2/01/2015 to get month only 02


SELECT DATEPART(d, '2/01/2015')   return 1
select DATEPART(m, '2/01/2015')    return 2
0
Comment
Question by:VBdotnet2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40615659
This is a lot easier if you are using SQL Server 2012...

However, since you cannot use the FORMAT function, your only recourse is CONVERT as in:
DECLARE @YourDate date = '20150201'
SELECT CONVERT(varchar(10), @YourDate, 101)
SELECT SUBSTRING(CONVERT(varchar(10), @YourDate, 101), 4, 2)
SELECT SUBSTRING(CONVERT(varchar(10), @YourDate, 101), 1, 2)
0
 

Author Comment

by:VBdotnet2005
ID: 40615664
Hi Anthony,
My string is 2/01/2015  not 20150201'.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40615669
This is how you could do it with SQL Server 2012:
DECLARE @YourDate date = '20150201'
SELECT FORMAT(@YourDate, 'MM/dd/yyyy')
SELECT FORMAT(@YourDate, 'dd')
SELECT FORMAT(@YourDate, 'MM')
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40615672
My string is 2/01/2015  not 20150201'.
What data type is it exactly in SQL Server?
0
 

Author Comment

by:VBdotnet2005
ID: 40615683
It is varchar(12).
0
 

Author Comment

by:VBdotnet2005
ID: 40615691
We have sql2008.

SELECT CONVERT(varchar(10), '2/01/2015'      , 101)
2/01/2015
SELECT SUBSTRING(CONVERT(varchar(10), '2/01/2015', 101), 4, 2)
1/
SELECT SUBSTRING(CONVERT(varchar(10), '2/01/2015', 101), 1, 2)
2/
0
 

Author Comment

by:VBdotnet2005
ID: 40615693
Can we return 01 for date and month 02?
0
 

Author Comment

by:VBdotnet2005
ID: 40615694
Also date should be 02/01/2015 format.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40615713
Try it this way:
DECLARE @YourVarcharDate varchar(12) = '2/01/2015'         
DECLARE @YourDate date = CONVERT(date, @YourVarcharDate, 101)
SELECT CONVERT(varchar(10), @YourDate, 101)
SELECT SUBSTRING(CONVERT(varchar(10), @YourDate, 101), 4, 2)
SELECT SUBSTRING(CONVERT(varchar(10), @YourDate, 101), 1, 2)

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40615731
I would like to point out that a string is always a string. Sounds simple enough until you see this:

'2/01/2015'

because it looks like a date, but it remains a string (note the single quotes!) and it is NOT a date unless it is converted.

To do what you want, it needs to become a date first and THEN you can use date functions such as DATEPART()

nb: In the database date/datetime/time information is NOT stored in a human readable manner but stored as sets of numbers.

so in addition to the comment above
DECLARE @YourVarcharDate varchar(12) = '2/01/2015'         
DECLARE @YourDate date = CONVERT(date, @YourVarcharDate, 101)
SELECT CONVERT(varchar(10), @YourDate, 101)
SELECT SUBSTRING(CONVERT(varchar(10), @YourDate, 101), 4, 2)
SELECT SUBSTRING(CONVERT(varchar(10), @YourDate, 101), 1, 2)

-- now date functions will work too

select datepart(month,@YourDate)
select datepart(year,@YourDate)
select datename(weekday,@YourDate)

Open in new window


no points please
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40616916
SELECT convert(varchar(2), cast('2/01/2015' as datetime), 101) as month,
    convert(varchar(2), cast('2/01/2015' as datetime), 103) as day

SELECT convert(varchar(2), input_date, 101) as month,
     convert(varchar(2), input_date, 103) as day
FROM (
    select cast('2/01/2015' as datetime) as input_date
) AS test_data
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

771 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