Date format dd/mm in SQL Server - sql

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]

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.

how to remove time from datetime

The field DATE in the database has the following format:
2012-11-12 00:00:00
I would like to remove the time from the date and return the date like this:
11/12/2012
First thing's first, if your dates are in varchar format change that, store dates as dates it will save you a lot of headaches and it is something that is best done sooner rather than later. The problem will only get worse.
Secondly, once you have a date DO NOT convert the date to a varchar! Keep it in date format and use formatting on the application side to get the required date format.
There are various methods to do this depending on your DBMS:
SQL-Server 2008 and later:
SELECT CAST(CURRENT_TIMESTAMP AS DATE)
SQL-Server 2005 and Earlier
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SQLite
SELECT DATE(NOW())
Oracle
SELECT TRUNC(CURRENT_TIMESTAMP)
Postgresql
SELECT CURRENT_TIMESTAMP::DATE
If you need to use culture specific formatting in your report you can either explicitly state the format of the receiving text box (e.g. dd/MM/yyyy), or you can set the language so that it shows the relevant date format for that language.
Either way this is much better handled outside of SQL as converting to varchar within SQL will impact any sorting you may do in your report.
If you cannot/will not change the datatype to DATETIME, then still convert it to a date within SQL (e.g. CONVERT(DATETIME, yourField)) before sending to report services and handle it as described above.
just use, (in TSQL)
SELECT convert(varchar, columnName, 101)
in MySQL
SELECT DATE_FORMAT(columnName, '%m/%d/%Y')
I found this method to be quite useful. However it will convert your date/time format to just date but never the less it does the job for what I need it for. (I just needed to display the date on a report, the time was irrelevant).
CAST(start_date AS DATE)
UPDATE
(Bear in mind I'm a trainee ;))
I figured an easier way to do this IF YOU'RE USING SSRS.
It's easier to actually change the textbox properties where the field is located in the report. Right click field>Number>Date and select the appropriate format!
SELECT DATE('2012-11-12 00:00:00');
returns
2012-11-12
Personally, I'd return the full, native datetime value and format this in the client code.
That way, you can use the user's locale setting to give the correct meaning to that user.
"11/12" is ambiguous. Is it:
12th November
11th December
For more info refer this: SQL Server Date Formats
[MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 101) from tbl
[DD/MM/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 103) from tbl
Live Demo
TSQL
SELECT CONVERT(DATE, GETDATE()) // 2019-09-19
SELECT CAST(GETDATE() AS DATE) // 2019-09-19
SELECT CONVERT(VARCHAR, GETDATE(), 23) // 2019-09-19
In mysql at least, you can use DATE(theDate).
You may try the following:
SELECT CONVERT(VARCHAR(10),yourdate,101);
or this:
select cast(floor(cast(urdate as float)) as datetime);
Use this SQL:
SELECT DATE_FORMAT(date_column_here,'%d/%m/%Y') FROM table_name;

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