June 27, 2016 at 9:29 am
I need a datetime field to report the value as '12/5/2016' or '3/2/2016'
( Takeway leading zeros from month and day )
The following works fine. But I wonder is there a short function already
to do that ... ?
This works fine
Declare @xx Datetime;
Set @xx = '02/05/2016'
SelectCASE WHEN LEFT (Replace((Convert(CHAR(10), @xx, 101 )), '/0', '/' ),1) = '0' THEN
SUBSTRING( (Replace((Convert(CHAR(10), @xx, 101 )), '/0', '/' )) ,2, 200 )
ELSE
Replace((Convert(CHAR(10), @xx, 101 )), '/0', '/' )
END
June 27, 2016 at 9:41 am
Not a big improvement, but might help.
SELECT STUFF( REPLACE( '/' + CONVERT(CHAR(10), @xx, 101 ), '/0', '/' ), 1, 1, '')
June 27, 2016 at 9:54 am
I would also like to say that hopefully this is for display purposes only (which could instead be handled by the client side application). Whatever you do, make sure you don't actually save these values back into your database. It will eventually come back to haunt you.
June 27, 2016 at 12:44 pm
YB makes a VERY key point: formatting stuff should be done outside SQL Server where at all possible!!! Don't waste any of the precious and expensive resources on your SQL Server doing that type of stuff!
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
June 27, 2016 at 1:31 pm
YB:
This is not for display purposes.
It is for a TAB delimited file that goes to another company.
I am not sure why they can not handle dates in the mm/dd/yyyy format.
I was given one of their sample files and I noticed dates in the m/d/yyyy format ( I mean the leading zeros gone for single digit dates and months )
It makes no sense.
Also there is no client side application when using this stuff with SSIS.
June 27, 2016 at 8:16 pm
mw112009 (6/27/2016)
YB:This is not for display purposes.
It is for a TAB delimited file that goes to another company.
I am not sure why they can not handle dates in the mm/dd/yyyy format.
I was given one of their sample files and I noticed dates in the m/d/yyyy format ( I mean the leading zeros gone for single digit dates and months )
It makes no sense.
Also there is no client side application when using this stuff with SSIS.
It shouldn't actually matter to the other company. Ask them instead of just going on based on a possible misunderstanding by someone else.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply