Multiple date format conversion to single format - sql

I have a column which saves date in a varchar format. I can't change it because it is a part of the existing system. I need to convert this to datetime because I need to apply the datediff function.
The problem is that the dates are in several different formats.
Example:
14/09/2013 dd/mm/yy
20-06-2014 dd-mm-yy
1/29/2013 mm/dd/yy
2013-05-30-15.10.04.812055
8/3/2012 4:22:16 PM dd/mm/yy or mm/dd/yy can't make out the difference
I thought of switching dd and mm but that will be confusing to identify especially with the 1st 3rd and last case. When I convert as is, it gives me an out of range error. How do I fix this?
I am currently using SQL Server 2008

Related

Extract "Mon-YY" from a date as a Date

I am using this code to extract month and a year from a date:
to_char(to_date(i.CREATION_DATE, 'DD-MON-YYYY'), 'Mon-YY') "Month"
It works as required, however, when I extract the report to .csv, the value returned (e.g. Jan-2021) is formatted as text. When I then load the report to Tableau it creates many issues as Tableau does not recognise "Jan-2021" as a date, but a text.
I have tried many solutions, including EXTRACT, or by placing to_date before the to_char (which resulted in "ORA-01858: a non-numeric character was found where a numeric was expected" error).
Please, how do I make this work? Thank you.
There's no need to convert this to text before bringing into Tableau. Leave it as a date. Make yourself with familiar with Tableau date functions and date formats, and you can solve this very simply.
If it's a date in Tableau, and you want to display as MMM-YY, simply set the default format of that field as a custom format of mmm-yy

SQL Server doesn't get date format dd-mm-yyyy

I'm trying to INSERT date variable into my SQL server.
The input format I'm trying to insert is: dd-mm-yyyy
And my SQL server column (which defined as type DATE) expect mm-dd-yyyy.
My PC date format is dd-mm-yyyy.
The error msg:
Conversion failed when converting date and/or time from character string.
Questions:
Why is the expected format mm-dd-yyyy and not dd-mm-yyyy?
How can I make the INSERT command work with this or another date format?
Thanks.
The format that your PC uses for dates doesn't matter when passing literal string for dates, the language setting for the LOGIN you are using to connect to SQL Server does. As the value is being interpreted as MM-dd-yyyy this very likely means that your language setting is (American) ENGLISH.
If, therefore, you are the only person that uses that LOGIN you may well want to change the language of your LOGIN to be appropriate for yourself. Most of the European languages, including BRITISH English (because the British don't speak English Microsoft? 😒) use the format dd-MM-yyyy.
Really, however, what you should be doing is using an unambiguous date format, of which there are only 2 in SQL Server, regardless of the language setting and data type. Those 2 formats are yyyyMMdd and yyyy-MM-ddThh:mm:ss.nnnnnnn. If you are always using the newer date and time data types (so not datetime or smalldatetime), then yyyy-MM-dd( hh:mm:ss.nnnnnnn) is also unambiguous, however, as it's language specific for the older data type then I don't normally recommend it's use.
Storage wise, date and time data types don't have a format, so your column isn't "expected" a value in the format MM-dd-yyyy, as it doesn't retain the "format data" that the value was passed in. Again, the only reason why a value like 13-12-2020 is working is because your language setting, which uses mdy for date interpretation.
Two things really,
Firstly, you can control the expected date format for each connection. Before issuing the insert statement, do set dateformat dmy; or set dateformat mdy; as appropriate.
Secondly, it's always advisable to work with dates in the ISO format of YYYYMMDD, so if you insert the data '20210314' it will always be treated correctly.

DATEDIFF working on yyyymmdd format SQL server

Why the DATEDIFF accepts yyyymmdd format ? Isnt it only for yyyy-mm-dd format ?
The function DATEDIFF(datepart, start_date, end_date) is working fine when the dates are in yyyymmdd format e.g.
DATEDIFF(DAY, 20161201, 20161231);
It gives answer 30
and if you do DATEDIFF(DAY, 20161201, 20161231) + 1 -- end date inclusive
It gives 31
Thanks,
Aiden
This can be a bit hard to tease out, but it is explained in the documentation, in the section "Supported String Literal Formats for date".
There are five sections on this (but the last two are API specific). The first section is affected by internationalization settings and has a bunch of different formats.
The second is the ISO 8601 format. I think the hyphenated format conflicts with the very last format in the first table ("yyyy dd mm" without the hyphens).
The final section has this informative line "A six- or eight-digit string is always interpreted as ymd". Hence, 'YYYYMMDD' is always interpreted correctly. Hence, this is the unambiguous format for date/time.
Note: Because the functions you are using expect dates, if the integer works, then it is converted to a date using its string representation. Normally, date constants should be in single quotes.
Actually its the sql server default format of dates but you can format your dates according to your requirements by following the link:
http://www.sql-server-helper.com/sql-server-2012/format-function-vs-convert-function.aspx

I have date stored in the format mm/dd/yyyy but i need to view as August 22nd, 2014

I have date stored in format mm/dd/yyyy in the database, but I need to view as Month Date(with superscript), Year. I am using Oracle 11g
The database just stores dates, in an internal format. It doesn't store them in mm/dd/yyyy, or anything human-readable.
You are sending them to the database in mm/dd/yyyy format, by the sound of things; but how you display them is down to your client application. You should be retrieving them from the database, and then formatting them at the client end.
In other words, don't do this in SQL at all.
The returned date can be manipulated as part of the query.
Example:
SELECT TO_CHAR(ts_col, 'DD-MON-YYYY') FROM date_tab;
The format string you would probably want is: 'MONTH DDTH, YYYY'
I would advise to just take whatever format the database gives you and handle it client-side though.
More information about TO_CHAR can be found in the oracle docs. A full list of format models can be found here. There is also Datetime Format Element Suffixes allowing for 2nd, 3rd, etc.
heres a work around if you want ... i dont know if this exactly what you want
SELECT REPLACE(TO_CHAR(SYSDATE, 'MonthDD,YYYY'),',','nd,') FROM DUAL;

how to reformat DateTime data type

I know that it is possible to reformat the DateTime data type of the current date and time by using select convert. However, I havent been able to find a method to reformat an existing column (DateTime data type) to: hh:mm:ss yyyy/mm/dd. Or even better, I would like to reformat it to show time only. I dont want to simply convert to time data type because I am working with a chart that accepts either Date or Date time. But what I really want to display on that specific axis of the chart is time. Is there any way to reformat DateTime to my requirements? Thanks.
SQL Server's convert function can take an optional style argument depending upon the datatype. This is how you can get a datetime converted to a string in a variety of formats. For example, try running:
select convert(varchar(30), getdate(), 108) -- returns hh:mi:ss
You can replace getdate() in this example with a column name as well.
There are many styles available, as shown in the documentation.
Note that you will be returning a varchar, so you may want to sort by the original column's datatype.
You can use SET DATEFORMAT but this will change all DateTime's on your SQL Server. You can do some custom formatting, but it will convert the DateTime into a VarChar, so you won't be able to treat it as a DateTime.
SET DATEFORMAT: http://msdn.microsoft.com/en-us/library/ms189491.aspx
I'm not sure if you will be able to use the format you're asking about, though.