Convert char to date - Hive - sql

I need concat 2 columns char in 1 column date.
I tried it:
INSERT INTO tb_teste PARTITION (dt_originacao_fcdr)
SELECT
tp_registro_fcdr,
seq_registro_fcdr,
tp_cdr_fcdr,
dt_atendimento_fcdr,
date_dt_atendimento_fcdr,
hr_atendimento_fcdr,
timestamp(from_unixtime(unix_timestamp(CONCAT(dt_atendimento_fcdr, hr_atendimento_fcdr), 'yyyyMMddHHmmss')), "yyyy-MM-dd HH:mm:ss") as date_hr_atendimento_fcdr,
duracao_atend_fcdr,
hr_originacao_fcdr,
duracao_total_fcdr,
duracao_chamada_tarifada_fcdr,
st_chamada_fcdr,
fim_sel_orig_fcdr,
numero_a_fcdr,
tp_numero_a_fcdr,
numero_b_fcdr,
tp_numero_b_fcdr,
numero_b_orig_fcdr,
numero_c_fcdr,
tp_numero_c_fcdr,
tp_trafego_fcdr,
esn_fcdr,
central_fcdr,
erb_fcdr,
tp_erb_fcdr,
face_erb_inici_fcdr,
erb_final_fcdr,
face_erb_final_fcdr,
erb_original_fcdr,
imsi_fcdr,
imei_fcdr,
tecnologia_fcdr,
cd_oper_ass_a_fcdr,
cd_oper_ass_b_fcdr,
cgi_fcdr,
nu_tlfn_fcdr,
tp_tlfn_fcdr,
tp_tarifa_fcdr,
ident_num_a_fcdr,
ident_num_b_fcdr,
cd_prestadora_fcdr,
cna_orig_ar_erb_fcdr
FROM tb_op_nor;
Result: date_hr_atendimento_fcdr 2019-03-03
The column containing the time and date is not null or empty.
Example:
Time zone: Brazil.
I need date and time in the same columns.

You can concat two fields then unix_timestamp and by using from_unixtimestamp function we can format the output timestamp.
with cte as (select stack(1,"20190303","131615") as (dt,hr)) --sample data
select
timestamp( --cast to timestamp
from_unixtime(unix_timestamp(concat(dt,hr),'yyyyMMddHHmmss'),"yyyy-MM-dd HH:mm:ss") --concat and change the format
)
from cte
Output:
2019-03-03 13:16:15.0
If you want to convert to brazil time to UTC or vice versa then use to_utc_timestamp/from_utc_timestamp.

Related

Invalid seconds in timestamp field in SQL

I've a timestamp field in a table where second values SUBSTR(col,13,2) are 60+ in some places.
I want to update invalid second portion of the timestamp field and convert this kind of data into valid timestamp format DDMMYYYYHHMISS.
Sample data:
CREATE VOLATILE TABLE TEST (COL VARCHAR(50)) ON COMMIT PRESERVE ROWS;
INSERT INTO TEST (04012022000010);
INSERT INTO TEST (31012022000066);
INSERT INTO TEST (02012021000067);
COL
1 31012022000066
2 02012021000067
3 04012022000010
That's #Kendle's logic in Teradata SQL:
select
cast(substring(col from 1 for 12) as timestamp(0) format 'ddmmyyyyhhmi') +
cast(substring(col from 13 for 2) as interval second) as TS_correct,
to_char(TS_correct,'ddmmyyyyhhmiss')
from test;
I think that this is what you are needing. We convert the string without the seconds to DATETIME and add the number of seconds.
I give 2 versions because the DATETIME format requested is not the standard ISO format
The first request uses the date format as requested in the question. I give 2 versions because I don't know whether your local settings modify the automatic functions.
DDMMYYYYHHMISS
SELECT CAST(
CONCAT(
SUBSTRING(COL,1,12),'00'
) AS TIMESTAMP)
+ INTERVAL SUBSTRING(COL,11,2) second
FROM TEST;
We convert the input to ISO and then format the result to requested format.
Input: YYYY-MM-DD HH:MI:SS
Output: DDMMYYYYHHMISS
SELECT CAST(
CONCAT(
SUBSTRING(COL,5,4),'-',
SUBSTRING(COL,1,2),'-',
SUBSTRING(COL,3,2),' ',
SUBSTRING(COL,9,2),':',
SUBSTRING(COL,11,2),':00'
) AS TIMESTAMP)
+ INTERVAL SUBSTRING(COL,11,2)
FORMAT 'DDMMYYYYHHMISS'
AS corrected_date
FROM TEST;

Custom ORDER BY in SQLite for string date

I have a column "date_time" and many other.
date_time is a string date (because in SQLite there is no date format), e.g. "2020.2.1" means 1st of February of 2020th.
I want to sort by this date 2020.1.1-2020.1.2-...-2020.12.31
I am using:
SELECT *
FROM Table
ORDER BY date_time
And I am getting sorted as a string:
here is an example:
2020.1.10
...
2020.1.29
2020.1.3
...
How I can sort it as a date if I am using SQLite and my date is string?
select *
from Table
order by cast(date_time as datetime)

How to delete first value in column at rest values convert to date

I have in SQL Server column "col1" where I have values like below:
col1
-----
1210607
1191011
1200101
I would like to convert this column to date column, by doing:
1210607 is 2021-06-07
1191011 is 2019-10-11
1200101 is 2020-01-01
So 1 is at the beginning of each row and it is useless, so I need to delete 1 at the beginning of each row and convert values to date format as above.
How can I do that in SQL Server?
You can add the century -- 19000000 -- then convert to a string and a date:
select convert(date, convert(varchar(255), 19000000 + col1))
Here is a db<>fiddle.

Convert/get varchar variable to YYYYMM

I have 4 CTE's in this table and the third one contains a DATETIME converted to VARCHAR (with format based on the requirement) as startDate in DD/MM/YYYY format. The last cte does calculations based on the data generated and one of the columns needs to store YYYYMM date based on startDate.
The problem it's getting the year and the month from this converted DATETIME, using convert() it shows this:
IDPER
-------
01/01/ --DD/MM/
These 2 show YYYYMM correctly when startDate isn't converted:
Select *, left(convert(nvarchar(6),new_ini,112),6) as IDPER from table
Select *, convert(nvarchar(6),new_ini,112) as IDPER from table
How could I get YYYYMM format having startDate converted? Or what could be a more smart approach to the requirement
If you have a string in the format DD/MM/YYYY and you want YYYYMM, then use string operations:
select right(new_ini, 4) + substring(new_ini, 4, 2)
You should be storing date values as dates or a related type, not as string. But given that you have already stored this as a string, string operations can do what you need.
My way would be slightly different
SELECT CONVERT(NVARCHAR(6), CONVERT(DATE, new_ini, 103), 112);
Here, I first converted it to date and then formatted to YYYYMMDD and taken 6 chars only
declare #date DATE = GETDATE();
select REPLACE(LEFT(CONVERT(DATE,#date,112),8),'-','') -- 1st approach
select FORMAT(#date,'yyyyMM') --2nd approach

order by not formatted date sqlite

I'm storing Dates as string in the database with this format DD-MM-YYYY.
When I tried to make a select query with an orderby on the date column. I didn't get the expected result.
example of result :
28/02/2013
27/02/2013
01/03/2013
My sql query :
SELECT * FROM data ORDER BY strftime('%s', date_column)
Thank you.
The problem is that you store dates as DD-MM-YYYY strings, which does not only prevent natural ordering of dates as strings, but also parsing them with SQLite's date and time functions. Click the link and scroll down to 'Time Strings' section.
SQLite expects date/time strings in the natural order, most significant digit to least significant, that is, YYYY-MM-DD. You can use string operations to transform your DD-MM-YYYY strings into that form. For instance:
select
substr(reversed_date, 7,4) || '-' ||
substr(reversed_date, 4, 2)|| '-' ||
substr(reversed_date, 1, 2) as proper_date
from (
select '12-03-2000' as reversed_date
)
;
You can either transform your date column into this format (as #peterm suggests) or just use the value of proper_date for sorting. You don't need to use strftime for that, but date-related functions will work with such values.
IMHO you need to change the format you store dates in from
DD-MM-YYYY
to
YYYY-MM-DD
From docs
Time Strings
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
...
Then your original query and this one will work as expected
SELECT * FROM Table1 ORDER BY date(date_column);
SELECT * FROM Table1 ORDER BY strftime('%s', date_column);
Output:
| date_column |
---------------
| 2013-02-27 |
| 2013-02-28 |
| 2013-03-01 |
sqlfiddle
According to the documentation the following should work
SELECT *
FROM data
ORDER BY strftime('%Y-%m-%d', date_column)
try:
SELECT * FROM data ORDER BY to_date(date_column)
probably this might solve you problem as it is going for string comparison rather than date comparison so
01/03/2013 appears smaller than 28/02/2013 or 27/02/2013
thus output is :
01/03/2013, 27/02/2013, 28/02/2013
This query worked for me filter dates
SELECT inst.*,
Substr(columnname, 4, 2) AS newdate,
Substr(columnname, 0, 3) AS newday,
Substr(columnname, 12, 5) AS newtime
FROM table_name AS inst
WHERE child_id = id
ORDER BY newdate DESC, newday DESC, newtime DESC