convert VARCHAR2 data to date in oracle - sql

I have the following data in TIMESTAMP column
TIMESTAMP - VARCHAR2
Format - YYYY-MM-DD hh24:mi:ss.0
2018-01-31 23:47:35.0
2018-01-01 00:00:48.0
2018-01-01 06:54:36.0
I'm trying to make a query to get data between two dates (example 4th Jan to 18th Jan) but im not sure how can i convert this into DATE format. Previously i used to get the data everyday and used WHERE TIMESTAMP LIKE '2018-01-04%' in my sql query.
Really appreciate if someone could assist

One method uses to_date():
where to_date(timestamp, 'YYYY-MM-DD HH24:MI:SS.F') >= date '2018-01-04' and
to_date(timestamp, 'YYYY-MM-DD HH24:MI:SS.F') < date '2018-01-09'
However, you are using a good date format, so you can use string comparisons:
where timestamp >= '2018-01-04' and
timestamp < '2018-01-09'
Although this is convenient, you should be storing the value as a date or timestamp. Storing date/time values as strings is generally bad (although your format makes this more reasonable).

Related

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool

How to fetch month from date where date column is in varchar datatype. FYI using snowflake tool.
For example if i want data of june month ? how can i fetch ?
You can use the TO_DATE(…) function to treat the VARCHAR column as a formatted date type, and the EXTRACT(…) function to retrieve just the month out of the date.
If your date string is formatted in a well-known manner, TO_DATE's automatic parsing (or a direct cast using the :: operator) will suffice, and you can write your query this way:
SELECT * FROM table
WHERE
EXTRACT(month, TO_DATE(varcharCol)) = 6 -- June of every year
AND EXTRACT(year, varcharCol::DATE) = 2020; -- June of 2020 alone
Alternatively, if the date is in a non-standard format, use available formatting options to make TO_DATE(…) parse it properly:
-- Dates of custom format, such as: 'June # 02 # 2020'
SELECT
EXTRACT(month, TO_DATE(varcharCol, 'MMMM # DD # YYYY')) AS month
FROM table
WHERE
month = 6;
Note: You can also swap all DATE and TO_DATE above with TIMESTAMP and TO_TIMESTAMP if the data carries a whole timestamp value within it instead of only a date.
First of all, you shouldn't store dates as strings. But you probably know that.
If you do store dates as strings, you store them all in one particular format, say, 'mm/dd/yyyy'. So, use a substring function to get the month digits.
For 'mm/dd/yyyy':
where substring(date_string, 1, 2) = '06'
For 'yyyy-mm-dd':
where substring(date_string, 9, 2) = '06'
In many situations you can also use LIKE:
For 'mm/dd/yyyy':
where date_string like '06%'
For 'yyyy-mm-dd':
where date_string like '%-06-%'
You have to use to_date in snowflake to convert varchar datatype to date as following
select *
from yourTable
where to_date(yourDateColumn, 'YYYY-MM-DD') >= '2020-06-01'
and to_date(yourDateColumn, 'YYYY-MM-DD') <= '2020-06-30'

Oracle SQL Select Current Timestamp without Timezone and 24hr Format

I have a Oracle SQL statement where I have to get the current timestamp as one of the columns. But I dont require the Timezone which CURRENT_TIMESTAMP gives or the AM/PM given by LOCALTIMESTAMP.
I require the current timestamp in 24hr format without the timezone.
Is it possible to get that in Oracle SQL?
It seems you're mixing 2 concepts here: "datatype" and "date format mask".
data type: LOCALTIMESTAMP returns datatype TIMESTAMP and CURRENT_TIMESTAMP returns datatype TIMESTAMP WITH TIME ZONE. TIMESTAMP is similar to DATE but has a higher precision. As usual... checking the docs is worth it.
date format mask: determines how you display the date information. Americans can't read 24 hour format, the rest of the world is confused by AM/PM. Fortunately, you can decide how you want to display the date as explained in the oracle docs.
If you just want to return the current date in 24 hour format you could do something like:
SELECT
TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS') as mydate,
<other columns>
FROM
<table_name>
If you need the date to be more precise and you require fractional seconds then you can use SYSTIMESTAMP instead of DATE with a format mask 'DD-MON-YYYY HH24:MI:SS.FF9'

SQL- Difference between TIMESTAMP, DATE AND TIMESTAMP WITH TIMEZONE?

What is the difference between TIMESTAMP , DATE AND TIMESTAMP with TIMEZONE?
E.g if I wanted to search for all entries between 01-JAN-1990 and 01-JAN-2000 , how would I do so in each format?
I have been searching for timestamp as:
SELECT COUNT(*) FROM TABLE_NAME WHERE DATE BETWEEN '01-JAN-1990' AND '01-JAN-2000;
But I am not sure what format to use to search for DATE or TIMESTAMP WITH TIMEZONE.
The data types and differences between them are in the documentation. The short version is:
DATE has precision down to a second with no time zone support;
TIMESTAMP has precision down to fractions of a second (up to nine decimal places, but your operating system affects that too), still with no time zone support;
TIMESTAMP WITH TIME ZONE has the same precision as TIMESTAMP but also has time zone support, as the name suggests;
TIMESTAMP WITH LOCAL TIME ZONE adjusts the stored value to and from the creating/querying session's local time zone.
You might find this article interesting too.
Whenever you are comparing datetime values stored in your database you should use values of the same datatype to compare against. You don't want to have to convert every value in the column for comparison, especially if the column is indexed. If you have a DATE column then compare with a DATE - don't compare as a string, and don't rely on implicit conversion of a string. When you do:
WHERE date_col BETWEEN '01-JAN-1990' AND '01-JAN-2000'
you are relying on your NLS_DATE_FORMAT being DD-MON-YYYY and your NLS_DATE_LANGUAGE being English. If someone else runs the same query in another session their settings may cause the query to fail (or in some cases, give wrong results, which can be worse). To avoid the language issue it's better to use month numbers rather than names. If you have a string variable to compare against you should use TO_DATE() to convert the string to a DATE using a fixed known format mask - don't rely on NLS. If you have a fixed value you can do the same, or you can use a date literal, which is shorter and unambiguous.
With the format you used you are also including any rows which have a the column set to midnight on January 1st 2000, but not any later on that day. That may be what you want, but make sure you understand how BETWEEN works. If you're actually looking for dates within that decade, including at any time on December 31st 1999, you can use:
WHERE date_col >= DATE '1990-01-01' AND date_col < DATE '2000-01-01'
For timestamps you can either use TO_TIMESTAMP() or a timestamp literal:
WHERE ts_col >= TIMESTAMP '1990-01-01 00:00:00'
AND ts_col < TIMESTAMP '2000-01-01 00:00:00'
For timestamps with time zones you can either use TO_TIMESTAMP_TZ() or a timestamp literal, with a names time zone region:
WHERE tstz_col >= TIMESTAMP '1990-01-01 00:00:00 America/New_York'
AND tstz_col < TIMESTAMP '2000-01-01 00:00:00 America/New_York'
Don't compare dates with strings. It can work if your session's nls_date_format happens to match the format of the string that you're using. But then your query will immediately fail for someone who has a different configuration. Compare dates with dates, timestamps with timestamps, etc.
For dates, you can use either ANSI date literals
SELECT COUNT(*)
FROM your_table
WHERE date_column BETWEEN date '1900-01-01' AND date '2000-01-01'
or you can use a to_date with an explicit format mask
SELECT COUNT(*)
FROM your_table
WHERE date_column BETWEEN to_date('1900-01-01', 'YYYY-MM-DD')
AND to_date('2000-01-01', 'YYYY-MM-DD')
Note that a date in Oracle always has a day and a time component. If you don't specify a time in your to_date, it will default to midnight. If you use an explicit to_date, you can use a string in any format just so long as it matches the format mask you pass in as the second parameter.
For timestamps, you can either use an ANSI timestamp literal
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN timestamp '1900-01-01 00:00:00.000'
AND timestamp '2000-01-01 00:00:00.000'
or you can use a to_timestamp with an explicit format mask
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN to_timestamp('1900-01-01 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FFF')
AND to_timestamp('2000-01-01 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FFF')
If you use an explicit to_timestamp, you can use a string in any format just so long as it matches the format mask you pass in as the second parameter.
For timestamps with time zone, as you may have guessed, you can either use an ANSI timestamp literal
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN timestamp '1900-01-01 00:00:00.000 -05:00'
AND timestamp '2000-01-01 00:00:00.000 -05:00'
or you can use the to_timestamp_tz function with an explicit format mask
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN to_timestamp('1900-01-01 00:00:00.000 -05:00', 'YYYY-MM-DD HH24:MI:SS.FFF TZH:TZM')
AND to_timestamp('2000-01-01 00:00:00.000 -05:00', 'YYYY-MM-DD HH24:MI:SS.FFF TZH:TZM')
If you use an explicit to_timestamp_tz, you can use a string in any format just so long as it matches the format mask you pass in as the second parameter.

Oracle to_timestamp

quick question on Oracle To_timestamp.
I have a two fields in a table that are both varchars
1 field contains the YYYYMMDD formatted value
the 2nd field contains the hh24mmss formatted value
Im trying to convert these two fields into a timestamp field,
expected output is DD/MM/YYYY HH24:mm:ss
like '7/23/2015 12:53:04'
however, it gaves me weird result...
like '7/15/0023 12:53:04'
seems year becomes day and day becomes year..
---------- heres my sql ----------------
select
to_date(to_char(to_date('20150723','yyyymmdd'),'yyyymmdd'),'yyyymmdd') dt,
to_char(to_date(SUBSTR('005304000',1,6), 'hh24miss'), 'hh24:mi:ss') tm,
TO_TIMESTAMP(
to_date(to_char(to_date('20150723','yyyymmdd'),'yyyymmdd'),'yyyymmdd') ||
to_char(to_date(SUBSTR('005304000',1,6), 'hh24miss'), 'hh24:mi:ss'), 'yyyy/mm/dd HH24:MI:SS' ) dttm
from dual
You have one layer to much in your conversion chain. And it will be much easier if you concatenate the two columns before converting them:
to_date(date_column||time_column, 'YYYYMMDDHH24MISS')
will return a date and a time (an Oracle DATE includes a time part).
Now that you have a proper date value, you can then format that as you want:
to_char(to_date(date_column||time_column, 'YYYYMMDDHH24MISS'), 'yyyy/mm/dd HH24:MI:SS')
As your "time" column doesn't contain milliseconds there is no real benefit in using to_timestamp.
But you should really fix your table, and store that information in a single DATE (or TIMESTAMP) column. Storing dates in varchar columns is always a very bad choice.