How to Convert String to Date in SQL Server? - sql

I have date column in my table as string type example Feb-18. How do I change it to date type as 01-02-2018?

Use convert() with add one day :
select convert(date, '01-' + 'Feb-18')

SQL Server is pretty good about figuring out dates. But you need a day, so:
select convert(date, '01-' + datecol)
Note: You should be very careful about storing dates as strings. I would recommend that you test the conversion to be sure it works for all values:
select datecol
from t
where try_convert(date, '01-' + datecol) is null and
datecol is not null;
If this returns any rows, then you have bad dates in your data. Oh, it would have been better to catch these by rejecting the insert/updates in the first place. However, you might be able to figure out how to fix them.

You can also try the following way.
Select try_cast('01-' + 'Feb-18' as Date) as [String To Date]

Related

Date Conversion in SQL

I have a date in following format in my DB.
10/16 - mm/yy
I need to convert it to:
October/16
Is this possible?
If it's not possible then please tell me why.
This is not a date, it's missing the day, it's a bad way to store year/month. There should be a 4 digit year to avoid confusion and the year should be listed first to enable correct sorting, e.g. '2016/10' or a numeric value 201610.
You can cast it to a DATE first and then use a FORMAT to disply only month/year:
set dateformat myd;
select format(cast(mystupidcolumn + '/1' as date), 'MMMM/yy')
Or SUBSTR the month part and use a CASE.
try this format,
SELECT DATENAME(month, DATEADD(month, #mydate-1, CAST('2008-01-01' AS datetime)))
You can display date by using this code
select datename(month, YourColumnName) + '/' + right(YEAR(YourColumnName),2)
FROM yourTableName
Simply change yourColumnName with name of your table column and yourTableName with name of table.
Yes you can, and it depend in what database you use to call date functions
If you column Datetime format
SQL server DATENAME(Month, GETDATE())
MySQL database MONTHNAME(now())
otherwise
convert it will in your choice at database or you code logic
split the value and lookup at month enum or fake the date to be accepted and complete date format like 01/10/16
so do something like SELECT DATENAME(Month, datecolumn) + '/' + YEAR (datecolumn)
also you can use instead of Year function DATEPART(yy,datecolumn)
the way you do it with format will look like
CONVERT(VARCHAR(11),GETDATE(),106)
but excepted to get first 3 char of month JUN

Removed Time from Date in SQL

I wrote the following query. This query returns two fields. Firstly, it returns an integer that represents a date, and it then converts that integer into a date.
select DISTINCT date_column,
cast(convert(varchar(10), date_column) as date) as [Week Label]
from table_one
order by date_column desc;
The conversion works well. However, the date that is returned contains a time. How can I get rid of the time?
You can simply use this to remove time part
convert(date, getdate())
OR
you can use this also
convert(varchar(10), getdate(),120)
convert(varchar(10), getdate(),101) doesn't have a time part, just the date.
I used http://www.w3schools.com/sql/func_convert.asp for the options but this one from MSDN may be better: http://msdn.microsoft.com/en-us/library/ms187928.aspx

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