Replace character with multiple characters - sql

In my application date formats are configurable and it is stored in one of the following formats in the table:
d.m.Y --> represents dd.mm.rrrr
d.m.y --> represents dd.mm.rr
Y.m.d --> represents rrrr.mm.dd
y.m.d --> represents rr.mm.dd
I need to use it in my queries and procedures to show the dates in the format specified by user which can be done by TO_CHAR(any_date, date_format)
but, I will need to convert these values to the format that Oracle can recognize.
Currently, I am using REPLACE to achieve this as follows:
TO_CHAR(<my_date_column>, REPLACE(
REPLACE(
REPLACE(
REPLACE('<date_format>',
'd', 'dd'),
'm', 'mm'),
'Y', 'rrrr'),
'y', 'rr'))
I am curious to know if there is another way of doing the same. May be using REGEXP_REPLACE or some other way.

Are there any other possible formats then the 4 you listed in your question?
If not that the decode of VBokšić is a valid answer.
Your replace is just as hardcoded.
I suggest to not store the format the user entered at all (neither the text value 19.09.19 nor the used format dd.mm.rr): let the user interface handle the interpretation and store a real DATE. Split your API from your data model.

Would use of decode be an option for you?
TO_CHAR(<my_date_column>, decode(<date_format>, 'd.m.Y', 'dd.mm.rrrr'
, 'd.m.y', 'dd.mm.rr'
, 'Y.m.d', 'rrrr.mm.dd'
, 'y.m.d', 'rr.mm.dd'))

You should start by looking at the definition of the column in the table. Is it a Date type field, a Number field, a varchar2 field?
If any_date is in Date format, then you can just use to_char:
to_char(any_date, 'd.m.Y')
if the field,any_date is in number(8,0) format, then, you need to understand the number format and translate it to char. Suppose any_date is a number as YYYYMMDD, then
use to_char(any_date) to get the number as YYYYMMDD.
If any_date is in number format as YYYYMMDD, and you want it as d.m.Y, then, your best bet is to convert the number to char, then convert to date, then convert back to character:
to_char(to_date(to_char(any_date), 'YYYYMMDD), 'd.m.Y')
If you date is in varchar2 format, then again, the best option is to convert to date format and convert back to varchar2:
to_char(to_date(any_date, ''), '')

Related

How to re-order number or change to be date format dd/mm/yyyy in PosgrestSQL?

My question, now I have table customer in Postgresql and contain the column name is update (for keeping track of update customer info date.)
The date format is ex:20170302 but I want to convert to be 02/03/2017.
Note: the datatype of the update is character varying.
I have tried several times to find all the solutions by google but not fix.
First, you should fix the data type to be a proper date or datetime. Don't store dates as strings!
But you are. You can convert the value to a date and then back to a string:
select to_char(to_date(update, 'YYYYMMDD'), 'DD/MM/YYYY')
The documentation contains the formatting elements that you can use.

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

How to convert date stored in nvarchar to date in MMDDYYY format removing dashes (e.g 06122012)?

I want to convert date stored as nvarchar in Birth_date column SQL.
For example, currently I have dates in 1999-01-22 format, I want to convert it to 01221999 (8 characters) using SQL Server.
Can anybody help me please?
I agree with #marc_s that you should probably be using the proper data type (date), but if you really have to return this as your custom format, we can make good use of convert:
select replace(convert(varchar(10), convert(date, Birth_date), 101), '/', '')
I suggest that it's a code smell that convert doesn't even have the format you're looking for. We must therefore massage the converted data, in this case using replace to remove slashes.

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

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