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

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

Related

Convert datetime string in datetime format in SQL server

I'm using MS SQL server and I have a date field of type text. The dates stored there are in this format
2017-03-01T18:23:02+0700
I'm trying to convert this field in a datetime field but I fail. I have tried
CONVERT(datetimeoffset,date, 127)
CONVERT(datetime,date, 127)
CONVERT(datetime2,date, 127)
but I keep getting
Conversion failed when converting date and/or time from character
string.
I think the problem is that according to ISO8601 the time offset must be in the format hh:mm while mine is hhmm. I don't mind keeping only the date (yyyy-mm-dd) if it is more easy.
I have read similar question but none matches exactly my case and I can't figure out the solution.
Try this
Declare #dt varchar(50)
set #dt = '2017-03-01T18:23:02+0700'
select convert(datetime, replace(LEFT(#dt, LEN(#dt) - 1), '+', '.'), 126)
If you need only date part then you can use below query
SELECT CAST(LEFT('2017-03-01T18:23:02+0700',10) as DATE)
Use Below query to convert datetime :
SELECT CONVERT(DATETIME,REPLACE(REPLACE('2017-03-01T18:23:02+070','T','
'),'+','.'),103)
For DATE only use below query :
SELECT CONVERT(DATE,REPLACE(REPLACE('2017-03-01T18:23:02+010','T','
'),'+','.'),102)
It doesn't seem to work in both ways (both raise error):
SELECT CONVERT(datetime,'2017-03-01T18:23:02+0700',127)
SELECT CONVERT(datetime,'2017-03-01T18:23:02+07:00',127)
It seems that it works only specifying Z as time zone:
SELECT CONVERT(datetime,'2017-03-01T18:23:02Z',127)

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")))

How to convert the date format in SQL Server?

I have a table that contains date and the format is :'01-16-1989' which is mm-dd-yyyy but I want to insert to another table that has format like this: '1989-01-16' which is yyyy-mm-dd. What function can I use in the insert statement to do this?
insert into des_table
select date from source_table
How to update the second line in order to finish the date format conversion?
You can convert a string from mm-dd-yyyy format to a date using conversion type 110:
select convert(date, [date], 110)
from source_table
You can then convert this back to a string in the yyyy-mm-dd format using code 120:
select convert(varchar(10), convert(date, [date], 110), 121)
You can format date with DATE_FORMAT()
This is one way
SELECT DATE_FORMAT(date, '%Y-%m-%d') FROM source_table;
Source: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
Unless they are not actually datetime data types, your insert should be golden as it is.
Also, to answer this you should tell us what RDBMS you are using.
If you are using MySQL this should get you covered:
select DATE_FORMAT(current_date,'%y/%m/%d');
On the dates note you should keep all your dates
ISO formated
Since your column is already a DATE you don't need to do any conversions on the insert. If you want it to look a certain way in a specific query result, you can use CONVERT. There are many format options, but again, you don't need to bother with changing how it looks on the insert.
Here are some resources
https://msdn.microsoft.com/en-us/library/ms187928.aspx
https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx
http://www.w3schools.com/sql/func_convert.asp

SQL date formats

How can I convert this 201402110544 to date(2014-02-11) in SQL server 2008?
You can cast as DATE data type (https://msdn.microsoft.com/en-us/library/bb630352(v=sql.100).aspx)
SELECT CAST(datetime_value AS DATE)
SELECT CAST(GETDATE() AS DATE) --> 2015-08-18
If you have a string to cast as DATE you can use:
SELECT CAST(LEFT('201402110544', 8) AS DATE)
You trim out the time part, by taking 1st 8 chars (YYYYMMDD) and will result a valid string to cast as DATE.
If this format is always the same, you can do this
DECLARE #d VARCHAR(20)='201402110544'
SELECT CAST(SUBSTRING(#d,0,9) as DATETIME)
Also have a look at The ultimate guide to the datetime datatypes which explains in detail about handling date-times
String to date conversion sure is a weakness of SQL Server. CONVERT does that, but can only deal with a number of given formats. So you must convert your string into such a format first and then convert it to date afterwards.
One such format is 120 = 'yyyy-mm-dd hh:mi:ss' which I think comes closest to yours. Another 102 = 'yyyy.mm.dd' suffices to get the date.
convert(
date,
concat(substring(datestring, 1,4), '.',
substring(datestring, 5,2), '.',
substring(datestring, 7,2)),
102)
from data;
SQL fiddle: http://www.sqlfiddle.com/#!3/9eecb7/3689
EDIT: I stand corrected. Horia is right; CAST supports 'yyyymmdd', so you can use it directly by merely cutting of the string's time part. This is more readable than my suggestion above, which makes CAST the better option in your case.

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.