I want to convert date which is in this format '2015-01-25'
to a format like this '25-JAN'
in sql server 2008 R2
Somebody Help me in this...
I want to convert date which is in this format '2015-01-25'
to a format like this '25-JAN'
in sql server 2008 R2
Somebody Help me in this...
You will want to use date style 6 for this and then use left
function to grab only day and month part like this:
--substitute @date with your own field
DECLARE @date date = '2015-01-25'
SELECT LEFT(convert(VARCHAR, @date, 6), 6)
UPDATE:
Also, if you are looking for a hyphen in between your day and month you could do something like this instead:
--replace space with a hyphen (-)
DECLARE @date date = '2015-06-24'
SELECT REPLACE(RTRIM(convert(VARCHAR(6), @date, 6)),' ','-')
If this format is for visual purposes like some kind of report, then great, but it is HIGHLY recommended that you do not store dates in any other format but the default YYYY-MM-DD'
Try this out. This adds leading zero for days under 10.
DECLARE @yourTable TABLE(dt DATE)
INSERT INTO @yourTable
VALUES ('2015-01-25'),
('2015-01-01'),
(GETDATE());
SELECT dt,
RIGHT('0' + CAST(DATEPART(DAY,dt) AS VARCHAR(2)),2)
+ '-'
+ LEFT(DATENAME(MONTH,dt),3),
DATEPART(YEAR,dt) yr --what about year???
FROM @yourTable