Converting to mm/dd/yyyy format - sql

I have a table called SF_Data and there is a column called IN_Date, ID the data looks like:
ID IN_Date
1 9/8/2010
2 26/04/2011
3 20/09/2010
The datatatype of IN_Date is varchar(50).
I am trying to convert the IN_Date to mm/dd/yyyy format. I tried doing this:
Select convert(varchar,IN_Date,103) From dbo.SF_Data
But still the format doesn't change. Can anyone tell me where I am going wrong

You need a convert to fix the data (to the correct datatype) before formatting...
Select
convert(varchar,
convert(date, IN_Date, 103),
101)
from dbo.SF_Data

The 3rd parameter to convert has no meaning when converting from varchar to varchar. So per #marc_s' comment, you'd have to convert the varchar to a datetime using the 103 format, and then from datetime to varchar specifying the 101 format:
Select convert(varchar(12),convert(datetime,IN_Date,103),101) From dbo.SF_Data
For example:
select convert(varchar(12),convert(datetime,'31/12/2001',103),101)
prints 12/31/2001.
See MSDN.

Related

How to convert dd-mmm-yy hh.mm nvarchar to any date format

I have a very specific format for date and time stored in my table as nvarchar.
The format is 'DD-MMM-YY hh.mm' ex. '30-NOV-20 19.25' , '22-JAN-52 13.34' etc.
Is there any way to convert the above nvarchar to standard datetime formats within particular table?
Select Convert(DateTime, Replace(YourDateColumn, '.', ':'))
From YourTable
Your format would be fine if you had a colon between the hours and minutes. Use the replace function to accomodate that.
When you select a datetime value in SQL Server Management Studio, you should understand that it is determining how to display the value. If you want it to display in another way, you need to convert it back to a string while applying a format. For example:
Select PERSON_BDAY,
Convert(VarChar(10), Convert(DateTime, Left(PERSON_BDAY, 9)), 1)
From YourTable

Convert from varchar into date in SQL Server

This looks easy solution but I can't seem to figure out as to why this is not working for me. I have a column that has data like this:
DateField
----------
12/16/2016
11/06/2016
All I want to do is to convert from varchar into a date column, but I am getting this error:
Conversion failed when converting date and/or time from character string.
Here is my simple query:
select convert (date, DateField) as convertedField
from myTable
Nothing wrong with the two examples you have given. There are some bad dates in your table which cannot be converted to date.
Use TRY_CONVERT function for bad dates it will return NULL
select TRY_Convert(date,DateField)
From myTable
You should always store dates in DATE/DATETIME datatype.
If you want to see the records which cannot be converted to date then
select DateField
From myTable
Where TRY_Convert(date,DateField) IS NULL
If working with a specific date format like mm/dd/yyyy You can specify it in Convert() function like the following
CONVERT(DATETIME,DATAFIELD,101)
If it still is not working, use TRY_CONVERT() to get which rows are throwing this exception:
SELECT *
FROM TBL
WHERE TRY_CONVERT(DATETIME, DATAFIELD, 101) IS NULL
This will return rows that cannot be converted
TRY_CONVERT() will return NULL if conversion failed
Read more about DateTime formats here:
SQL Server CONVERT() Function tutorial
Read TRY_CONVERT MSDN Article
You need to specify the format of date time while formatting. The date in your table is currently in U.S format so you should pass the third argument 101 in your convert function.
SELECT CONVERT(date,[DateField],101) FROM myTable;
Working Fiddle here http://rextester.com/NYKR49788
More info about date time style here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

How to convert column type from str to date when the str is of format dd/mm/yyyy?

I have a large table in sql I imported from a large csv file.
A column is recognized as a str when it contains date information of format dd/mm/yyyy.
I tried select TO_DATE('12/31/2015') as date but that does not work because TO_DATE function needs yyyy-mm-dd format.
How can I rearrange the '12/31/2015' string to '2015-12-31' format inside sql so that I can convert the column type to date?
I am doing this on a sparkSQL (on databricks environment ) due to the very large size of the data where the update keyword of sql does not seem to be supported.
Just re-read your question;
I would suggest this:
UPDATE table
SET column = Convert(varchar(10), Convert(smalldatetime, column, 103), 120)
This converts the column value to smalldatetime, using the british format (dd/mm/yyyy), then converts it back to varchar, using the 120 format (yyyy-mm-dd); The 120 format contains time info, but this will be truncated because it's being cast back as varchar(10);
Test it:
SELECT Convert(varchar(10), Convert(smalldatetime, column, 103), 120)
FROM table
The below link answer works
https://forums.databricks.com/answers/12121/view.html
df.withColumn("tx_date", to_date(unix_timestamp($"date", "M/dd/yyyy").cast("timestamp")))

convert varchar to datetime field

I'm trying to filter some records by date but the datetime field seems to be stored as varchar and I'm struggling to convert it. Below is a sample of the data:
ID DateField
0002 14/04/1989 01:30
0003 16/04/1989 09:45
0004 16/04/1989 06:00
0005 19/04/1989 01:07
0006 21/04/1989 16:03
When I use
cast(Datefield as datetime)
I get the following error message:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
Edit - this does actually display results in datetime format but with the error message, why is that?
What's the best way to convert my datefield to valid datetime format? Thanks
Use CONVERT with Style
CONVERT(DATETIME,Datefield ,103)
To find bad data, see what this returns
set dateformat dmy
select Datefield from table where isdate(where)=0
You should always use proper DATETIME datatype to store datetime values
Try this. You need to add style part to identify the format
SELECT CONVERT(DATETIME, dates, 103)
FROM (VALUES ('14/04/1989 01:30'),
('16/04/1989 09:45'),
('16/04/1989 06:00'),
('19/04/1989 01:07'),
('21/04/1989 16:03')) cs (dates)
see CAST and CONVERT
you need to use
select CONVERT(datetime,your_Datefield ,103)
because your your_Datefield holding the datetime value in dd/mm/yy format.
follow the link, you'll get the explanation.
You can get the error because of by default datetime format is mm/dd/yyyy. So If your value have this format, then it easily convert without error.
For this you have to convert with format which convert the string into proper date-time format. For this cast will not work , but convert work. Here is link for more details.
As your format is dd/mm/yyyy, you need to use 103 format. Just check this to understand.
Select convert( datetime, getdate(), 103)

I want to convert a whole column into the date format of yyyymmdd, I do not want the current date

I want to convert a whole column into the date format of yyyymmdd, I do not want the current date, thus I cannot use getdate() command, there is already data in the column, I just need the right command to convert the whole column into yyyymmdd format.
The column I am using is FIELD_034 and the table is Sur_CompassAuto1_1_7_fetch.
I am using SQL Server.
Thank you
I fully agree with the paqogomez's response, but instead of date style of 111, better use 112.
The output for the statement
SELECT CONVERT(VARCHAR(10), FIELD_034, 111)
will result in yyyy/MM/dd. Where as,
SELECT CONVERT(VARCHAR(10), FIELD_034, 112)
will return yyyyMMdd, which is what he needed.
A common misconception is that a datetime has a format. If you are storing your dates as a datetime, then you can output it in any format.
It sounds as though you might be storing your values as a Varchar. You would be better off converting your varchar dates into a datetime, then you can do whatever you want with them
That said, if you HAVE FIELD_034 as a DateTime, then its as easy as
SELECT CONVERT(VARCHAR(10), FIELD_034, 112)
from Sur_CompassAuto1_1_7_fetch
If the field is a varchar its similar:
SELECT CONVERT(datetime, FIELD_034, 112)
from Sur_CompassAuto1_1_7_fetch
The difficulty of this one is if you have your values in different or nonstandard formats. Then it would require some clean up to make the query work.
Edit: as #AArnold says, its 112 instead of 111. Date formats are subtle.