I have a date column which have dates in these formats dd/mm/yyyy, d/m/yyyy.
So basically if the month is January it will have it as 1 instead of 01.
But if the month is October it will have it as 10.
How can i convert that column to be in yyyy/mm/dd format?
For example convert 1/1/2021 to 2021/01/01.
You can do it with string functions and implicit conversions of strings to integers:
SELECT SUBSTR(datecol, -4) || '/' ||
SUBSTR('0' || (SUBSTR(datecol, INSTR(datecol, '/') + 1) + 0), -2) || '/' ||
SUBSTR('0' || (datecol + 0), -2) date
FROM tablename
Change datecol to the name of your column.
If you want to update the column it is better to use the format 'YYYY-MM-DD' which is the only valid text date format for SQLite:
UPDATE tablename
SET datecol = SUBSTR(datecol, -4) || '-' ||
SUBSTR('0' || (SUBSTR(datecol, INSTR(datecol, '/') + 1) + 0), -2) || '-' ||
SUBSTR('0' || (datecol + 0), -2)
See a simplified demo.
Related
I have a data set that has dates like this:
MM DD YY
2 8 10
3 9 11
4 10 12
I'm trying to write a query that displays the dates in one single column as MM/DD/YY
Any help would be greatly appreciated thanks
If you dont mind the data format, you can concatenate them.
select MM || '/' || 'DD' || '/' || YY DATE_COL from T
you can also convert it to date
select to_Date(MM || '/' || 'DD' || '/' || YY, 'MM/DD/YY') DATE_COL from T
Teradata actually stores dates as integers, as explained in the documentation:
Teradata Database stores each DATE value as a four-byte integer using the following formula: (year - 1900) * 10000 + (month * 100) + day.
If you want to generate a result of date datatype from your numbers, you can just do:
cast((yy + 100) * 10000 + mm * 100 + dd as date)
On the other hand, if you just want a string in format mm/dd/yy, it is probably simpler to use string functions; we just need to pad the 1-digit values with 0:
lpad(mm, 2, '0') || '/' || lpad(dd, 2, '0') || '/' || lpad(yy, 2, '0')
You can do below, assuming your year is in the 21st century.
select to_Date(lpad(MM,2,'0')|| '/' ||lpad(DD,2,'0')|| '/20' ||lpad(YY,2,'0'),'MM/DD/YYYY') as Date_field
from my_Table;
Here is a demo in Oracle - https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=1ba6814d793f2b8b269611e348540f6c. This would work in Teradata as well.
I have a column in my table having date value as Decimal like 20180715 for the date 15-07-2018.
I want to convert it to MMDDYYYY format.
Example:
Given decimal value 20180715 is converted to 07152018.
How to do it ?
try somthing like this:
select
VARCHAR_FORMAT( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD') , 'MMDDYYYY')
from yourtable
but you you want a really date do it:
select
DATE( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD'))
from yourtable
Try this query for the date
select
date(timestamp_format(char(yourcolumn+19000000), 'YYYYMMDD'))
from yourtable
To get the time
select
cast( substr( right( '00' || yourcolumn, 6) ,1,2) || ':' || substr( right( '00' || yourcolumn, 6) ,3,2) || ':' || substr( right( '00' || yourcolumn, 6) ,5,2) as time)
from yourtable
I am looking to convert one date format to another using SQL. I am using DB Browser for SQLite and the dates are stored in a column of type 'TEXT'.
Here are 2 examples of the current format:
4/17/2017 9:09:09 AM
10/4/2017 10:21:13 PM
Note that in the current format for the day, month and hour they currently do not pad with a leading '0' if only a single digit. Also they are putting the month first, then the day, then the year.
These 2 examples should be converted to the YYYY-MM-DD HH:MM:SS (24 hour) format:
i.e.
4/17/2017 9:09:09 AM -> 2017-04-17 09:09:09
10/4/2017 10:21:13 PM -> 2017-10-04 22:21:13
etc.
If anyone could provide me with a SQL query that can convert all values in a column like the first format into the second format that would be greatly appreciated. Thanks!
So I don't know much about python so had to go with the 'obscene chain of instr() and substr() commands'. It's a mess, but it seems to work now.
instr(DATE,'/') used to find the position of the first '/'
length (rtrim(DATE, 'PMA0123456789: ')) used to find the position of the second '/'
instr(DATE, ':') used to find the position of the first ':'
Well below is the mess I created :D
UPDATE testtable
SET DATE =
substr (DATE, length (rtrim(DATE, 'PMA0123456789: '))+1,4) || '-' ||
CASE instr(DATE, '/')
WHEN 2 THEN
'0'
ELSE ''
END
|| substr (DATE,1,instr(DATE,'/')-1) || '-' ||
CASE (length (rtrim(DATE, 'PMA0123456789: '))) - instr(DATE,'/')
WHEN 2 THEN
'0'
ELSE ''
END
|| substr (DATE,instr(DATE,'/')+1, length (rtrim(DATE, 'PMA0123456789: ')) - instr(DATE,'/')-1)
|| ' ' ||
CASE substr(DATE,length(DATE)-1,2)
WHEN 'AM' THEN
CASE substr(DATE, instr(DATE, ':')-2,2)
WHEN '12' then
'00'
ELSE
CASE substr(DATE, instr(DATE, ':')-2, 1)
WHEN ' ' THEN
'0'
ELSE
substr(DATE, instr(DATE, ':')-2,1)
END
|| substr(DATE, instr(DATE, ':')-1,1)
END
WHEN 'PM' THEN
CASE substr(DATE, instr(DATE, ':')-2,2)
WHEN '12' THEN
substr(DATE, instr(DATE, ':')-2,2)
ELSE
CAST (substr(DATE,instr(DATE, ':')-2,2) AS INT) + 12
END
ELSE
'error'
END
|| ':' || substr(DATE,instr(DATE, ':')+1,5);
I have a sqlite DB and the column dateID is stored as an integer e.g. 20131101 being Nov 1, 2013. I need to calculate # of days from the start of a period - say beginning of a period into a dayNumber column.
Here is my sql:
update temptable set dayNumber = dateid - (select min(dateid) -1 from temptable)
It was doing fine until I cross months e.g. 20131101 - (say) 20131017 and I get 85 instead of the 15 I had hoped for - Can anyone help me with the date conversions so I can get 15?
If you had your dates stored in one of the supported date formats, you could use the built-in date functions to do calculations on these values.
To convert from yyyymmdd to yyyy-mm-dd, use an expression like this:
substr(dateid, 1, 4) || '-' || substr(dateid, 5, 2) || '-' || substr(dateid, 7, 2)
To convert a date into a number of days, use, for example, julianday:
UPDATE temptable
SET dayNumber = julianday(substr(dateid, 1, 4) || '-' ||
substr(dateid, 5, 2) || '-' ||
substr(dateid, 7, 2)) -
(SELECT julianday(MIN(substr(dateid, 1, 4) || '-' ||
substr(dateid, 5, 2) || '-' ||
substr(dateid, 7, 2))) - 1
FROM temptable)
I'm looking to convert an integer to Days using a db2 database. The integers are in this format 20130101 or YYYYMMDD. I believe you have to write a custom function after converting the integer to a char but I was unsure of how to do the second conversion to DAYS. I'm looking for a returned format January, 1, 2013 from 20130101.
WITH
/*****************************************************
*** Sample Data ***
*****************************************************/
sample_data
( START_DATE , END_DATE ) AS
(
VALUES
(20130101, 20131227 )
, (20130930, 20131230 )
, (20130411, 20130912 )
, (20130410, 20140101 )
)
,
t2(START_DATE, END_DATE) AS
( SELECT
CAST(SUBSTR(START_DATE, 1,4) CONCAT '-'
CONCAT SUBSTR(START_DATE, 5,2) CONCAT '-'
CONCAT SUBSTR(START_DATE, 7,2) AS CHAR(15)),
CAST(SUBSTR(END_DATE, 1,4) CONCAT '-'
CONCAT SUBSTR(END_DATE, 5,2) CONCAT '-'
CONCAT SUBSTR(END_DATE, 7,2) AS CHAR(15))
FROM SAMPLE_DATA
)
SELECT
START_DATE,
END_DATE
FROM t2
You can use this:
select monthname(to_date(20130101, 'YYYYMMDD')) || ', ' ||
day(to_date(20130101, 'YYYYMMDD')) || ', ' ||
year(to_date(20130101, 'YYYYMMDD')) from sysibm.sysdummy1
The result is:
January, 1, 2013
Replace the integer 20130101 by your field name.
If you will be using the conversion in several places, it's probably better to create a function to avoid repeating the field and conversions.
You could also cut a few corners using aritmetics to get year and day, such as this:
select monthname(to_date(20130101, 'YYYYMMDD')) || ', ' ||
mod(20130101, 100) || ', ' || to_char(20130101 / 10000) from sysibm.sysdummy1
The result is the same.
It's a lot of casting, but you can use the TIMESTAMP_FORMAT function:
date(timestamp_format(char(start_date),'YYYYMMDD'))
Keep in mind that this just gets you a value that is an actual DATE, not necessarily in the "pretty" format that you list above.