March 10, 2009 at 7:55 am
Hi can we have an alternative of
CStr(Format([InvoiceDate],"dd-mmm-yyyy")) in SQL
Many Thanks ....
March 10, 2009 at 8:48 am
Using Books On Line (BOL) look up CAST and CONVERT (Transact-SQL)
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/a87d0850-c670-4720-9ad5-6f5a22343ea8.htm
DECLARE @InvoiceDate DATETIME
DECLARE @Ans AS VARCHAR(10)
SET @InvoiceDate = '2-29-08'
SET @Ans = CONVERT ( VARCHAR(12) , @InvoiceDate , 105 )
SELECT @Ans
which gives: 29-02-2008
March 10, 2009 at 10:45 am
but 29-02-2008 needs to be in 29-Feb-2008 format
March 10, 2009 at 10:49 am
How about the following:
DECLARE @InvoiceDate DATETIME
DECLARE @Ans AS VARCHAR(12)
SET @InvoiceDate = '2-29-08'
SET @Ans = CONVERT ( VARCHAR(12) , @InvoiceDate , 105 )
SELECT @Ans
SET @Ans = CONVERT ( VARCHAR(12) , @InvoiceDate , 113 )
SELECT @Ans
March 10, 2009 at 3:28 pm
Hi
If you don't need the conversion for direct inserts or updates into other tables maybe think about formattin in your client application ;).
Greets
Flo
March 10, 2009 at 3:42 pm
Hello nabajyoti.b,
Just to notice, there is NO such format as you wish.
The nearest format is the previously mentioned, but you need modification
DECLARE @InvoiceDate DATETIME
DECLARE @Ans AS VARCHAR(12)
SET @InvoiceDate = '3-11-09'
SET @Ans = CONVERT ( VARCHAR(12) , @InvoiceDate , 113 )
--This gives '11 Mar 2009', all you need is to replace spaces with '-'
--and here it is
SELECT @Ans
SET @Ans = REPLACE(CONVERT ( VARCHAR(12) , @InvoiceDate , 113 ),' ','-')
SELECT @Ans
--The result is '11-Mar-2009'
March 10, 2009 at 8:15 pm
But, again, and as Flo suggested... SQL Server is NOT the place to do such formatting unless it's for direct output to a file sans any trip through a GUI or for reporting being done on the server (again, sans any GUI or reporting software). There are many reasons but one of them is local... if you simply send the date back as a datetime datatype, the app will likely be able to automatically format the date based on local requirements setup on the desktop. If you brute force the format into a string, like you're doing, either the conversion must be reversed to be displayed correctly, or you end up with an incorrect display.
So... what are you going to use this display related conversion for? 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply