How to convert 3 type of dates? - sql

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

Related

Convert 8 Digit Number into Date in SQL

I have a date field that is pulling from a server as just an 8 digit number, and I would like to convert it to a standard date format.
For example, "20140501" would be converted to 05-01-2014
I tried using the DateTime code, but that didn't work for me. Any suggestions?
Many databases might recognize the string '20140501' as a date, because this is an ISO standard format. Hence, you might try this:
select cast(cast(datecol as varchar(255)) as date)
Whether or not this works depends on your database.

Does TO_DATE function in oracle require strict format matching

I am confused with to_date(char[,'format'[,nls_lang]) function. Lets suppose I have to_date('31-DEC-1982','DD-MON-YYYY');should the format specified in the function be same as the date string? The above function works fine. When I use to_date('31-DEC-1982','DD-MM-YYYY'); also works fine but the month field in date string and that in format does not match.
So my doubt is should the date string and format specified match exactly to convert it to Date object.
Thanks
Generally speaking, yes, the date string and specified format should match. But the reason why it works in your case is that Oracle, for certain cases, provides flexibility of alternative format matching.
Excerpt from official Oracle Site
If a match fails between a datetime format element and the corresponding characters in the date string, then Oracle attempts alternative format elements
So as per above table, you can use 'MON' or 'MONTH' in place of 'MM'.
Similarly you can use 'YYYY' in place 'YY', etc
Reference:
Oracle Format Matching
Whatever format you follow, the object returned will be of date type.
You can test this via creating a dummy table and showing the table description.
e.g. CREATE TABLE TEST AS(
select to_date('31-DEC-1982','DD-MON-YYYY') dd from dual);
Now desc test;
Result will be dd date.
Similar will be the result with another type.
However if you are using SQL Developer, the date will be show in the exact NLS format as the setting there applies.
tools->preferences->database->NLS

Store DateTime as culture independent in SQL Server 2008

I currently have a challenge of storing a DateTime value in a NVarChar field so that it's culture independent.
I've read that you can convert the value to an int by using CONVERT(int, GETDATE(), 112) which should make it culture independent but the former statement doesn't store the time.
What is the industry standard of storing a DateTime as culture independent?
EDIT
Please note that I can't use DateTime in my scenario. It must be NVarChar.
EDIT 2
Alright, found the answer to my own question.
To convert a DateTime to it's binary(8) raw format:
convert(binary(8), GETDATE())
I then store the value in a VARCHAR field as follows:
CONVERT(VARCHAR(MAX), convert(binary(8), GETDATE()), 2)
To retrieve it back from the varchar field and convert it to DateTime:
CONVERT(DateTime,CONVERT(binary(8), [TextField], 2))
As var as I'm concerned, this will store a DateTime as culture independent.
EDIT 3
It seems like user Kaf has the best solution. I will rather use format 126 to convert it to text and then back to DateTime from text.
Thanks everyone and sorry for the confusion.
If you CANNOT store date as Datetime, you can use style 126 which gives ISO8601 format (yyyy-mm-ddThh:mi:ss.mmm (no spaces)). I think it is culture independent.
Fiddle demo
select convert(nvarchar(50),getdate(),126)
Best thing is to store Date as a DateTime/Date type.
You should use DATETIME or DATETIME2 data type to store date and time values. They are stored in binary format in the database and are culture independent.
You can read more on MSDN here: http://msdn.microsoft.com/en-us/library/ms187819(v=sql.100).aspx
More on how SQL Server stores the datetime values: "It uses 8 bytes to store a datetime value—the first 4 for the date and the second 4 for the time." (from: http://sqlmag.com/sql-server/solving-datetime-mystery)
I do not get this idea to store a date in a varchar field so that it is 'culture independant'. dateTime data type is culture independant. What is culture dependent is the way date values are displayed:
MM/dd/YYYY
dd/MM/YYYY
YYYY-MM-DD
etc
But, if the display changes, the underlying value itself is still the same ... and this is why you can easily 'convert' dates from one format to another....
So, for the sake of simplicity, I do strongly advise you to switch to a culture-independant, datetime field. Otherwise any further use of this field's content (calculation, display, print out, etc) will be a real PITA ...

creating table in Oracle with Date

I want to create a table in Oracle 10g and I want to specify the date format for my date column. If I use the below syntax:
create table datetest(
........
startdate date);
Then the date column will accept the date format DD-MON-YY which I dont want.
I want the syntax for my date column to be MM-DD-YYYY
Please let me know how to proceed with this.
Regards,
A DATE has no inherent format. It is not simply a string that happens to represent a date. Oracle has its own internal format for storing date values.
Formats come into play when actual date values need to be converted into strings or vice versa, which of course happens a lot since interactively we write dates out as strings.
The default date format for your database is determined by the settings NLS_DATE_FORMAT, which you probably have set to DD-MON-YYYY (which I believe is the default setting for American English locales). You can change this at the database level or for a single session for convenience, but in general it is safer programming practice to be explicit so that you don't get errors or, worse, wrong results if your code is run in a different environment.
The simplest way to specify a date value unambiguously is a date literal, which is the word 'date' followed by a string representing the date in YYYY-MM-DD format, e.g. date '2012-11-13'. The Oracle parser directly translates this into the corresponding internal date value.
If you want to use a different format, then I recommend explicitly using TO_CHAR/TO_DATE with your desired format model in your code. Examples:
INSERT INTO my_table (my_date) VALUES ( TO_DATE( '11-13-2012', 'MM-DD-YYYY' ) );
SELECT TO_CHAR( my_date, 'MM-DD-YYYY' ) FROM my_table;
dates rdo not have a format like you're suggesting. they are stored internally as a 7 byte number. to format the date when selecting, please use TO_CHAR(yourdatefield, 'format')
where formats are all shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
eg to_char(startdate, 'mm-dd-yyyy')

character_length Teradata SQL Assistant

I have to run column checks for data consistency and the only thing that is throwing off my code is checking for character lengths for dates between certain parameters.
SEL
sum(case when ( A.date is null or (character_length(A.date) >8)) then 1 else 0 end ) as Date
from
table A
;
The date format of the column is YYYY-MM-DD, and the type is DA. When I run the script in SQL Assistant, I get an error 3580 "Illegal use of CHARACTERS, MCHARACTERS, or OCTET_LENGTH functions."
Preliminary research suggests that SQL Assistant has issues with the character_length function, but I don't know how to adjust the code to make it run.
with chareter length are you trying to get the memory used? Becuase if so that is constant for a date field. If you are trying to get the length of the string representation i think LENGTH(A.date) will suffice. Unfortanatly since teradata will pad zeros on conversions to string, I think this might always return 10.
UPDATE :
Okay so if you want a date in a special 'form' when you output it you need to select it properly. In teradata as with most DBs Date are not store in strings, but rather as ints, counting days from a given 'epoch' date for the database (for example the epoch might be 01/01/0000). Each date type in teradata has a format parameter, which places in the record header instructions on how to format the output on select. By default a date format is set to this DATE FROMAT 'MM/DD/YYYY' I believe. You can change that by casting.
Try SELECT cast(cast(A.date as DATE FORMAT 'MM-DD-YYYY') as CHAR(10)) FROM A. and see what happens. There should be no need to validate the form of the dates past a small sample to see if the format is correct. The second cast forces the database to perform the conversion and use the format header specified. Other wise what you might see is the database will pass the date in a date form to SQL Assitant and sql assitant will perform the conversion on the application level, using the format specified in its own setting rather then the one set in the database.