Solved

Date format in sql

Posted on 2015-02-17
11
446 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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 48

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 500 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

730 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