Converting different dates format into single date format - sql

In my application client can provide date in any formats and I have to display given date in dd/mm/yyyy format only.The date formats are as-
1. dd-mmm-yyyy (Ex- 21-Jan-2019)
2. dd/mm/yyyy (Ex- 02/03/2019)
3. mm/dd/yyyy (Ex- 04/23/2019)
4. dd.mm.yyyy (Ex- 08.02.2019)
5. dd mmmm yyyy (Ex- 13 March 2019)
6. yyyy-mm-dd (Ex- 2019-03-04)
7. mmmm dd, yyyy (Ex- January 23, 2019)
Pls suggest how can I use regex for identifying date format and then convert into desired format.

It has already been mentioned that your options (2) and (3) pose an existential problem for solving this problem. So, this answer ignores that problem.
SQL Server is pretty good about figuring out dates. So, you can try:
select try_convert(date, col)
This will return NULL values for unknown date formats. You can also look for particular formats:
select coalesce(try_convert(date, col),
try_convert(date, col, 101),
try_convert(date, col, 104),
try_convert(date, col, 120),
. . .
)
The expressions can be more complicated than try_convert() if you need to munge the string value before attempting a conversion.

The example for format 2 in your question is a great example of why this can't be done, i.e. it could be interpreted as either format 2 or 3.
You would need better control over your incoming data, which I think would necessarily require a manual element.
You could perhaps have a rule around which clients are sending dates in which format, if you are pulling some kind of client name/ID into the database, and then you could run the string value through a date conversion function which would be tailored to a specific client or group of clients, but even then, you'd have to trust that this would remain consistent over time.

When reading those invoices, can't you find the countryCode and instead of setting rules on each vendor, make rules by country?
SELECT CONVERT(VARCHAR(n), GETDATE(), style)
Style Standard
100 Default for datetime and smalldatetime
110 USA
111 JAPAN
112 ISO
https://tableplus.io/blog/2018/09/sql-server-date-format-cheatsheet.html

Related

MSAccess BETWEEN AND functions giving dates outside of range

I'm new to MS Access and having trouble with using the BETWEEN AND function when applying to dates.
SELECT EmployeeID, FirstName, LastName, StartDate
FROM Chefs
WHERE StartDate BETWEEN #01/12/2015# AND #31/12/2015#;
The above gives me entries with the correct dates as well as entries with dates before the range, but not after.
Changing to
WHERE StartDate BETWEEN #30/11/2015# AND #31/12/2015#;
gives me the correct answer. Can someone tell me why the first query doesn't work?
EDIT: Working with date format dd/mm/yyyy. DataType is Date/Time with General Date format.
The reason for your trouble is that Access SQL first tries if the format mm/dd/yyyy makes sense. 01/12/2015 does (reads as 2015-01-12), while 30/11/2015 does not. Next it tries dd/mm/yyyy which will succeed for 30/11/2015 (reads as 2015-11-30). Finally, it will try yyyy/mm/dd.
Thus, as Hans writes:
Many experienced Access developers default to a yyyy-mm-dd to avoid
ambiguity.
However:
If you don't like that, dd mmm yyyy should work.
is doomed to fail in any non-English environment, as mmm will be localized.
So stick to the ISO sequence yyyy-mm-dd or use DateSerial:
WHERE StartDate BETWEEN DateSerial(2015, 12, 1) AND DateSerial(2015, 12, 31);

Why does SQL Server convert VARCHAR to DATETIME using an invalid style?

I can't make out from the documentation why SQL Server parses a text in a format other than the specified style.
Regardless of whether I provide text in the expected format:
SELECT CONVERT(DATETIME, N'20150601', 112)
or incorrect format (for style 113):
SELECT CONVERT(DATETIME, N'20150601', 113)
The results are the same: 2015-06-01 00:00:00.000 I would expect the latter to fail to convert the date (correctly).
What rules does it employ when trying to convert a VARCHAR to DATETIME? I.e. why does the latter (incorrect format style) still correctly parse the date?
EDIT: It seems I've not been clear enough. Style 113 should expect dd mon yyyy hh:mi:ss:mmm(24h) but it happily converts values in the format yyyymmdd for some reason.
Because the date is in a canonical format ie(20150101). The database engine falls over it implicitly. This is a compatibility feature.
If you swapped these around to UK or US date formats, you would receive conversion errors, because they cannot be implicitly converted.
EDIT: You could actually tell it to convert it to a pig, and it would still implicitly convert it to date time:
select convert(datetime,'20150425',99999999)
select convert(datetime,'20150425',100)
select convert(datetime,'20150425',113)
select convert(datetime,'20150425',010)
select convert(datetime,'20150425',8008135)
select convert(datetime,'20150425',000)
And proof of concept that this is a compatibility feature:
select convert(datetime2,'20150425',99999999)
Although you can still implicitly convert datetime2 objects, but the style must be in the scope of the conversion chart.
Reason why is the date N'20150601' converted to valid datetime is because of fact that literal N'20150601' is universal notation of datetime in SQL Server. That means, if you state datetime value in format N'yyyymmdd', SQL Server know that it is universal datetime format and know how to read it, in which order.
You should convert to varchar type in order to apply those formats:
SELECT CONVERT(varchar(100), CAST('20150601' as date), 113)
OK, you are converting datetime to datetime. What did you expect? In order to apply formats you should convert to varchar and you have to have date or time type as second parameter.

SELECT CONVERT(VARCHAR(10), GETDATE(), 110) what is the meaning of 110 here?

When we convert or cast date in sql, see below sql code
SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]
it works fine, I just want to know the meaning of 110 in above code. what it does actually, sometimes we use 102, 112 etc. what is the use of that number.
That number indicates Date and Time Styles
You need to look at CAST and CONVERT (Transact-SQL). Here you can find the meaning of all these Date and Time Styles.
Styles with century (e.g. 100, 101 etc) means year will come in yyyy format. While styles without century (e.g. 1,7,10) means year will come in yy format.
You can also refer to SQL Server Date Formats. Here you can find all date formats with examples.
110 is the Style value for the date format.
TSQL Date and Time Styles
When you convert expressions from one type to another, in many cases there will be a need within a stored procedure or other routine to convert data from a datetime type to a varchar type. The Convert function is used for such things. The CONVERT() function can be used to display date/time data in various formats.
Syntax
CONVERT(data_type(length), expression, style)
Style - style values for datetime or smalldatetime conversion to character data. Add 100 to a style value to get a four-place year that includes the century (yyyy).
Example 1
take a style value 108 which defines the following format:
hh:mm:ss
Now use the above style in the following query:
select convert(varchar(20),GETDATE(),108)
Example 2
we use the style value 107 which defines the following format:
Mon dd, yy
Now use that style in the following query:
select convert(varchar(20),GETDATE(),107)
Similarly
style-106 for Day,Month,Year (26 Sep 2013)
style-6 for Day, Month, Year (26 Sep 13)
style-113 for Day,Month,Year, Timestamp (26 Sep 2013 14:11:53:300)
10 = mm-dd-yy
110 = mm-dd-yyyy
SQL Server CONVERT() Function

How to convert 3 type of dates?

I want to create a function, which has a paramater ( a string which contains a date ) and then the function converts it and returns it. In our company we have workstations with three different languages. We have hungarian, english and german workstations too. I want to read a date from the registry, but this date will be written into the registry according to the current regional setting.
So if the regional setting is hungarian, then date written to date registry is 2012.01.25 (YYY.MM.DD), but if i change the regional setting to german then the value written to the registry will be 25/01/2012 (MM.DD.YYYY). If i change the regional setting to english, then the value will be 01/25/2012 (DD.MM.YYYY).
Unfortunately i don't know which regional setting was used when the date was written into the registry, because it can be changed since the value was written into the registry.
This iy why i want to create a function which gets a date, and then converts it to this format: YYYY.MM.DD. but i don't know how to do it.
Could someone help me how to do this?
Thanks!
Are you managing this registry value directly or it belongs to another software and just trying tomread it?
If it's yours, then
A. if it is a string value, then simply format it before storing, to ISO format (yyyy-MM-dd), format or formatdate function will do the trick
B. if it is a binary value, convert the datetime value to double with cdbl and store that
Well, if it's not yours, then it's not your lucky day. I've done it a couple of years ago and I used the text around the date to make an assumption on the format ...
You can use this expression to convert your strings to SQL type date. This expression uses the DD/MM/YYYY format only when it cannot use its default MM/DD/YYYY format.
CASE
WHEN CHARINDEX('/', val)=5 THEN CONVERT(date,val,111)
ELSE CONVERT(date, val, CASE WHEN LEFT(val,2) <= '12' THEN 101 ELSE 103 END)
END
This expression should be used inside a select statement. I am assuming that the name of your varchar column containing a date string is val.
If you are saving those dates on a date type of column (DATETIME, DATE, SMALLDATETIME, etc), then it won't matter how it is displayed or under what language it was saved. If you want to convert a DATE to a VARCHAR on the format YYYY.MM.DD then you can use: CONVERT(VARCHAR(10),YourDate,102).

Formatting Dates In SqlServer To Use As Comparison

This should be easy
I have a date column on table A
SELECT * FROM TABLEA
WHERE DTE = '01/02/2010'
The problem is that this is deployed onto US servers and I have no control over the date as its an arg.
How can I tell SqlServer to treat this date as being in that format??
I gave tried this:
SELECT *
FROM TABLEA
WHERE DTE = CONVERT(VARCHAR(10), '01/01/2010' , 101) AS [DD/MM/YYYY]
Your last try was almost correct, but it should have been
SELECT * FROM TABLEA WHERE AS_OF_DATE = CONVERT(DATETIME, '01/01/2010', 101)
Use a safe format. For dates (without a time component), the safe format is YYYYMMDD, e.g. today is '20100209'.
For datetimes, the safe format is YYYY-MM-DD'T'HH:mm:SS, where 'T' is the literal T, so right now is '2010-02-09T11:10:30'.
(When I'm saying safe, I mean that SQL Server always, unambiguously, knows how to convert these strings into datetime values)
Check out this reference article: The ultimate guide to the datetime datatypes
EDIT: Specifically what Tibor says about SET DATEFORMAT & SET LANGUAGE, since you mention that you have no control over the input format.
Another option is a double conversion (check performance when used as criteria):
select strTempNo, dtmTempDateStart,
convert(varchar(10), Convert(datetime, dtmTempDateStart, 103), 126) As UTCDate
I use 103 here as the data is already in UTC format but this works as well (UTC ISO8601 is 126).
If your dates are known to be always in American format you have to use 101.
Alternatively use 112 (ISO "safe" format) and cut the first 8 characters out of the string.
Data sample: (Sorry, don't have an American date table available)
521002 2008-09-1500:00:00.000 2008-09-15
580195 2008-04-1500:00:00.000 2008-04-15
530058 2008-09-2200:00:00.000 2008-09-22
580194 2008-04-0200:00:00.000 2008-04-02
500897 2008-07-0100:00:00.000 2008-07-01
500966 2008-09-2300:00:00.000 2008-09-23