How to display monthname and year in SQL Server - sql

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.

Related

Convert date to varchar in SQL Server

How do I convert a column which is date type to varchar?
Sample data:
ENDDATE (DATE TYPE)
'1947-12-01 00-00-00'
Requested results:
ENDDATE (VARCHAR)
121947
If I understand the question correctly, you need the ENDDATE of value '1947-12-01 00-00-00' as 121947. You can use the below query
SELECT RIGHT(MONTH(ENDDATE)*1010000+YEAR(ENDDATE),6)
If you are working with 2012 version or higher, you can use format. For earlier versions you can use convert with some string manipulations:
DECLARE #D as date = '1947-12-01'
SELECT REPLACE(RIGHT(CONVERT(char(10), #d, 103), 7), '/', '') As charValue2008,
FORMAT(#d, 'MMyyyy') as charValue2012
Results:
charValue2008 charValue2012
121947 121947
Please note that Format runs relativley slow, so if you have a lot of rows you might want to choose another way to do that.

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

Date format dd/mm in SQL Server

I use SQL Server and I need to display a datetime data type in the following format:
dd/mm
day-month without the year, which is the most effective way?
Use 103 style in convert function and remove the year
SELECT LEFT(CONVERT(VARCHAR(15), Getdate(), 103), 5) --11/03
You'll find this site really helpful I think:
http://www.sql-server-helper.com/tips/date-formats.aspx
From that link, you can see this as a quick way to get DD/MM:
SELECT CONVERT(VARCHAR(5), GETDATE(), 3) AS [DD/MM]

Format a datetime field

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)

Converting date format sql

I am taking information from one database with the datetime string formatted like this
2011-08-25 13:53:22.607
and would like to format it to look like
8/25/2011 01:53:22 PM
Please suggest a way for me to do this.
in Sql Server try:
DECLARE #YourDatetime datetime
SELECT #YourDateTime='2011-08-25 13:53:22.607'
SELECT
CONVERT(varchar(2),month(#YourDateTime))+'/'+CONVERT(varchar(2),day(#YourDateTime))+'/'+CONVERT(varchar(4),year(#YourDateTime)) --date remove leading zeros
+' '+RIGHT('0'+LTRIM(LEFT(RIGHT(convert(varchar(50),#YourDateTime,109),14),8)),8) --amp/pm time format keep leading zeros
+' '+RIGHT(convert(varchar(50),#YourDateTime,109),2) --am or pm
output:
----------------------
8/25/2011 01:53:22 PM
(1 row(s) affected)
There is a good article on date formatting in SQL Server here: http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
It was written with SQL Server 2005 in mind but the information is still relevant.
There is no native m/d/yyyy style. You can get close (leading 0s on month and day, no leading 0 on hour) with this:
DECLARE #d DATETIME = '2011-08-25 13:53:22.607';
SELECT CONVERT(CHAR(10), #d, 101) + ' '
+ LTRIM(RIGHT(CONVERT(VARCHAR(25), #d, 22), 12));
You can do a lot more work to manipulate the strings to get exactly the format you're after, but for now I'd suggest doing that formatting in the application/presentation tier instead of arm-wrestling SQL Server into doing it.
In Denali you will be able to use FORMAT() with much more control and less ugly string manipulation (a.k.a. manual labor), e.g.
SELECT FORMAT(#d, 'M/d/yyyy hh:mm:ss tt');