Format a datetime field - sql

I have a table tblTest which has a field called e_date which has a type of DateTime.
I would like to know is there a method which would allow me to format in SQL so that this field only shows the date in 'd mmm yyyy' when the stored procedure is ran.
On SQL server 2008 R2
I have found a method to do this which show the date as i would please, but when i try and changing the format so that it only shows the date, the data associated with it does not show.
CONVERT(VARCHAR(9), [Date_ex], 6) AS ExamDates,

Look for a date_format function. In MySQL it would be something like:
//Syntax
DATE_FORMAT({date},{format})
//Example
DATE_FORMAT("2012-04-27",'%d/%m/%Y')
More info here

Take a look at Pinal Dave's function in this article. http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions/
For intance, after you create the function, you can call it by the following
select [dbo].[ufsFormat] (getdate(),'mm/dd/yy')
This is MS SQL though. You did not mention what RDBMS you are using.

Assuming MSSQL based on you history; for SQL Server 2012 simply use FORMAT()
Otherwise its a bit fiddly to drop a leading zero;
select
substring(case when day(e_date) < 10 then '' else '?' end + convert(char(11), e_date, 106), 2, 32)

Related

Conversion function to convert the following "11/30/2014" format to Nov-2014

How to covert the following 11/30/2014 into Nov-2014.
11/30/2014 is stored as varchar
Try it like this:
DECLARE #str VARCHAR(100)='11/30/2014';
SELECT FORMAT(CONVERT(DATE,#str,101),'MMM yyyy')
The FORMAT function was introduced with SQL-Server 2012 - very handsome...
Despite the tags you set you stated in a comment, that you are working with SQL Server 2012, so this should be OK for you...
A solution that should work even with Sql server 2005 is using convert, right and replace:
DECLARE #DateString char(10)= '11/30/2014'
SELECT REPLACE(RIGHT(CONVERT(char(11), CONVERT(datetime, #DateString, 101), 106), 8), ' ', '-')
result: Nov-2014
You could use TRY_CONVERT (To avoid breaking if you have any invalid dates) with the style 101 to convert to date, then FORMAT to get your desired output.
SELECT FORMAT(TRY_CONVERT(DATE, '11/30/2014', 101), 'MMM-yyyy')
HOWEVER dates should be stored as dates, and formatting should be left to the presentation layer, so what I would do is sort out your database so that the data is stored as the appropriate type, then you can format the data in your application. This will be a bit of work upfront but will solve a lot of headaches down the road.
It is also worth noting that FORMAT doesn't scale particularly well
select CONVERT(varchar,cast('11/30/2014' as date), 103)
Reference: https://msdn.microsoft.com/en-us/library/ms187928.aspx

How to display monthname and year in SQL Server

If date is 10/16/2015, column is datetime type '2015-10-16 10:09:19.443'
How to display only the month and year as ' Oct-15' in SQL Server?
If you are using SQL Server 2012 or later, you can use the Format() function:
Declare #Date DateTime = '10/16/2015'
Select Format(#Date, N'MMM-yy')
Result:
Oct-15
Edit - in light of #lad2025's comment, if necessary, you may need to also add the en-US locale:
Declare #Date DateTime = '10/16/2015'
Select Format(#Date, N'MMM-yy', 'en-US')
If you are trying to run a quick ad-hock query to see results formatted as MMM-YY, but do not have access to FORMAT function (i.e. use MS SQL Server 2008 or earlier) you can do this:
SELECT replace(right(convert(varchar(9), date_column, 6), 6), ' ', '-')
FROM my_table
However, if you are writing an application, and would like to present the date to end-user in this specific format, you should do the formatting in the host language.
Here's a pretty simple (and quick) way to convert from DATETIME, although I agree with other comments and answers that a parameter should really be kept in the canonical datetime format, that way any date handling is portable across languages:
SELECT RIGHT(REPLACE(CONVERT(VARCHAR(9), CAST('2015-10-16 10:09:19.443' AS DATETIME), 6), ' ', '-'),6) AS [Mon-YY]
Look at DatePart method in SQLServer and extract in postgreSQL.

Temporarily change format of a date column in SQL Query output

I am pulling data from SQL Server, and the date on our back end is formatted prtty ugly, like so:
2014-01-10 00:00:00.000
I was wondering if there is some sort of funciton that I can run in my query to display the date column differently in my output? I don't want to actually alter the way in which the Column is formatted on the back end, I just want to change how it is presented to the user in the results of a sigle query.
Thank you for your help!
Edit: I am using SQL server 2008 R2 as my RDBMS
SQL 2012: Has FORMAT():
SELECT FORMAT(Date_Field,'ddd, MM yyyy')
Here's a list of FORMAT() options
Prior to SQL 2012:
SELECT CONVERT(VARCHAR(12),Date_Field,109)
List of CONVERT() styles
If you just want to remove the time portion you can:
SELECT CAST(Date_Field AS DATE)
You can format datetime output using the CONVERT function. Review the MSDN documention and the W3Schools page for the Convert function for more details and examples.
Here is quick example of a few different formats available.
SELECT CONVERT(VARCHAR(19), GETDATE())
SELECT CONVERT(VARCHAR(10), GETDATE(),10)
SELECT CONVERT(VARCHAR(10), GETDATE(),110)
SELECT CONVERT(VARCHAR(11), GETDATE(),106)
SELECT CONVERT(VARCHAR(24), GETDATE(),113)
Results:
Feb 27 2014 11:54AM
02-27-14
02-27-2014
27 Feb 2014
27 Feb 2014 11:54:33:977

Date without the time

I'm working with SQL Server 2005.
I have a column called purchase_time of type datetime. How do I select this column with the time part - just the date.
Thanks,
Barry
EDIT:
Would it be safe to get the datetime and split it via Python on the first space, or is this format locale dependant?
In versions < 2008 (which, based on other comments to some of the answers, I believe you are running), the most efficient way is to keep it as a datetime type and use date math to avoid string conversions.
SELECT DATEADD(DAY, DATEDIFF(DAY, '20000101', purchase_time), '20000101')
FROM dbo.table;
EDIT
If you want the date only for display purposes, not for calculations or grouping, that is probably best handled at the client. You can do it in SQL simply by saying:
SELECT dt = CONVERT(CHAR(10), purchase_time, 120)
FROM dbo.table;
In SQL Server 2008 you can use the newly added date type:
select convert(date, purchase_time) from TableName
Update:
In versions prior to SQL 2008, I used the following solution for this problem:
select convert(datetime, convert(int, convert(float, purchase_time)))
from TableName

Oracle to SQL server Date conversion

I would like to convert an Oracle SQL query into SQL server query.
But I encountered a problem with the following line :
AND to_date(to_char(M_DATE,'DD-MM-YYYY')) = '27/01/12'
M_DATE : DATE NOT NULL
I use
to_char(DATE,'DD-MM-YYYY')
in order to get their data like that : DD-MM-YYYY 00:00:00.000 (data are stocked like : 25/02/12 15:32:06.578)
So I searched on the Internet, but I didn't find any available solution. But I'm not an experienced SQL user, so if anybody know the solution..
Thanks
In general when removing any time values from a date I would use Date functions rather than converting to string
DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))
instead of
CONVERT(VARCHAR, GETDATE(), 103)
Although the end result is the same you are maintaining date format and while I have no specific results sets to prove it conclusively I have found this to be much quicker when dealing with large quantities of data.
In Oracle, I would remove the time element of a datetime using trunc - like so:
AND trunc(M_DATE) = ...
In SQLServer, I would convert to a date - like so:
AND convert(date,M_DATE) = ...
SELECT CONVERT(VARCHAR(25), GETDATE(), 131)
You could just do:
AND convert(varchar(8), M_DATE, 3) = '27/01/12'
Of course, that won't work if you have dates from other centuries.
I'm not sure what you mean by "data are stocked like"; be aware that the Microsoft SQL Server DATE type only has a precision of one day. If you want to have the time as well as the day, you should use the DATETIME2 type