I have a date column in source which is defined as varchar(255). I want to convert that to timestamp(0). I am using the below query:
SEL CAST(CAST( SERIND AS CHAR(10)) || ' 00:00:00' AS TIMESTAMP(0))
FROM DP_BOX.SOC1
WHERE SERIND ='20130518'
I am getting invalid timestamp here. Sample SERIND are
20170509
00000000
Can anyone please help me on this?
You can try CAST with correct format of TIMESTAMP(0)
SEL CAST(SERIND as TIMESTAMP(0) FORMAT 'YYYYMMDDHHMISS')
FROM DP_BOX.SOC1;
You're going to have to CAST() your way to a date format that is acceptable as a component of a timestamp in Teradata. (I think there may be a shortcut with math to get to a numeric format as well but I have to noodle on that one.)
Assuming SERIND is CHAR(8) to begin with. The first CAST() gets you to a DATE value with a compatible format. The second CAST() gets you a DATE with 10 character format so that when you concatenate it to the TIME portion of the timestamp the resulting character conversion of the DATE value is in the correct 10 character format to be explicitly CAST() into a TIMESTAMP value:
SELECT CAST(CAST(CAST(SERIND AS DATE FORMAT 'YYYYMMDD')
AS DATE FORMAT 'YYYY-MM-DD') || ' 00:00:00' AS TIMESTAMP(0))
FROM DP_BOX.SOC1
WHERE SERIND > '00000000'; -- Invalid Date value
Related
I have a table including date column and hour column which is an integer type column varying from 0 to 24. I need to combine these two fields and create an hourly composite datetime field.
However, I was able to create that kind of variable by using || and cast. But I am unable to transform this code to Hive editor syntax. Can you help me with this problem
SQL Code:
CAST(CAST(CAST(DATE_OF_TRANSACTION AS FORMAT 'yyyy-mm-dd') AS VARCHAR(11))||' '||CAST(CAST( BasketHour AS FORMAT '99:') AS VARCHAR(10))||'00:00' AS TIMESTAMP(0)) Date_Time
Thank you very much
For example like this:
cast(concat(DATE_OF_TRANSACTION, ' ', lpad(BasketHour ,2,0),':00:00.0' ) as timestamp)
Im having problem in query
i cannot get the proper result dates i want
I need to get all the date from today or less than date today
can you help me or give me right query to use
for example this is mytable
id(number) datestring(string)
1 02/06/2019
2 02/06/2019
3 02/14/2019
4 02/04/2019
5 03/17/2019
query i use: SELECT * FROM mytable WHERE datestring <= date('now')
expected result is
id(number) datestring(string)
1 02/06/2019
2 02/06/2019
4 02/04/2019
Thank you for helping
The issue you are encountering is that date('now') will return 2019-02-06 , as such as all rows in the table start with 0 and that this is less than 2.
You either need to convert on of the dates to be in the same format as the other or use the same format when storing the data.
SQLite itself has various date format that can be recognised and used and it is advisable to utilise one of these as reduces necessary complexity.
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
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
SQL As Understood By SQLite - Date And Time Functions
The following is a solution that could be used to convert the datestring column within the query :-
SELECT * FROM mytable WHERE substr(datestring,7,4)||'-'||substr(datestring,1,2)||'-'||substr(datestring,4,2) <= date('now');
The following could be used to convert the existing data to a recognised format :-
UPDATE mytable SET datestring = substr(datestring,7,4)||'-'||substr(datestring,1,2)||'-'||substr(datestring,4,2);
This could be followed by your original query
SELECT * FROM mytable WHERE datestring <= date('now');
I have a table in Sqlite DB having two fields Id and Date (Date is of type Text).
I have stored a few dates in the table from c#. now i want to get the records matching specific day, month and year.
The query i have tried is:
select strftime('%m', Date) from testTbl Where id = 3;
also:
select Date(substr(Date, 0, 10)) as daa from testTbl Where id = 3;
but the result of these two quires is always null.. can anyone help me to sort this out?
Proposed (immediate) fix
Use the following select
select substr(Date, 0, 10) as daa from testTbl Where id = 3;
Cause of the issue
The problem (if you surround the above substr with a Date function) is that you're using a Text type that is not in the expected format
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
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
Alternative (better) approach
Anyway IMHO, it would be better to create the column with a Date type and to insert values in the following way
insert into testTbl values (DateTime("2015-12-31"),3);
so that you'll be able to do
SELECT strftime('%m/%d/%Y',Date) from testTbl where id = 3;
or also
SELECT Date from testTbl where Date > DateTime('2016-01-01');
from C# the parameterized command would be similar to
"insert into testTbl values (DateTime(?),?);"
with a parameter value myDate.ToString("yyyy-MM-dd")
Anyway you can actually get the month string with substr(Date,0,2) and the year with substr(Date,5,4) with your current format. I'm simply suggesting an alternative that I would find more standard (using the built-in Date format)
I have a column of data type TEXT:
date
----
DD/MM/YYYY
but I want to convert all rows in the column to:
date
----
YYYY-MM-DD 00:00:00
(Yes, 00:00:00 for all rows)
Is there any way to do it in SQLite?
Use strftime.
strftime('%Y-%m-%d %H:%M:%S', date_str);
EDIT:
Yes, my first quess do not work. This one does, though:
SELECT
date,
substr(date,7,4)||'-'||substr(date,4,2)||'-'||substr(date,1,2)||' 00:00:00' as text_repr,
datetime(substr(date,7,4)||'-'||substr(date,4,2)||'-'||substr(date,1,2)||' 00:00:00') as datetime_repr
FROM
t
Simply put - You have to parse it on Your own, as stated here or here...
I'm new to PostgreSQL (I have been using MS SQL for many years) and need to convert a numeric column which contains a time in seconds to HH:MM:SS format.
I have Googled and found that to_char(interval '1000s', 'HH24:MI:SS') works so I am attempting to use this with my field name:
to_char(fieldname, 'HH24:MI:SS') gives an error cannot use "S" and "PL"/"MI"/"SG"/"PR" together
to_char(fieldname::interval, 'HH24:MI:SS') gives an error cannot cast type numeric to interval
Can anyone show me where I am going wrong?
SELECT TO_CHAR('1000 second'::interval, 'HH24:MI:SS')
or, in your case
SELECT TO_CHAR((mycolumn || ' second')::interval, 'HH24:MI:SS')
FROM mytable
To convert seconds to HH:MM:SS
SELECT 120 * interval '1 sec';
You will get 00:02:00
To convert minutes to HH:MM:SS
SELECT 120 * interval '1 min';
You will get 02:00:00