Format date information from string with SQL - sql

Is there a way to convert a string like this '160806CD01' into a date like this '2016-08-06 00:00:00' with SQL where the year, month, and date are 16, 8, and 6 respectively?

The principle is to first extract the part of the string that contains the date, then use a conversion function to turn the string portion it into a date datatype.
The functions to use do vary depending on the RDBMS ; here are some examples :
Oracle :
TO_DATE(SUBSTR(col, 1, 6), 'YYMMDD')
MySQL/MariaDB :
STR_TO_DATE(SUBSTR(col, 1, 6), '%y%m%d')
SqlServer :
CAST(CONCAT('20', SUBSTRING(col, 1, 6)) as datetime)
Postgres :
TO_DATE(SUBSTRING(col, 1, 6), 'YYMMDD')

Related

Combine concat with substr in a sql query

I am doing a sql query (in DB2) and I need to extract a date that is in the following form:
2022-01-01
In the Where condition I am using:
CONCAT(SUBSTR('$P!{FLIB}', 1, 4), SUBSTR('$P!{FLIB}', 5, 2), SUBSTR('$P!{FLIB}', 9, 2))
Where '$P!{FLIB}' is the date, but I get the following error:
Invalid number of arguments for CONCAT function.
It should be like 20220101
As said in a comment, a different approach is to first take the entire 'date' substring, and then replacing the '-' with empty string:
SELECT REPLACE(SUBSTR(x, 1, 10), '-', '')
FROM (VALUES('2022-01-01'))V(x)
db<>fiddle

How can I adjust the Snowflake function to get date with error that it cannot parse?

I'm trying to convert numbers in this format 1180106 to dates.
I've been able to run the following query to get a YYYYMMDD format until today:
TO_DATE(1900 + LEFT(A.ODT_ENTERED_DATE, 3) || SUBSTR(A.ODT_ENTERED_DATE, 4, 4), 'YYYYMMDD')
The inner query builds a number like this: 2018.000000106
But then the TO_DATE function cannot create a date from it. I'm expecting 20180106
Instead I get the following error:
Can't parse '2018.000000106' as date with format 'YYYYMMDD'
If you just run:
select 1900 + LEFT(A.ODT_ENTERED_DATE, 3)
this returns the result as a varchar where the result returned is: 2018.00000
Cast to number in either of the following ways should give you the expected result :
select TO_DATE(1900 + TO_NUMBER(LEFT(1180106, 3))|| SUBSTR(1180106, 4, 4), 'YYYYMMDD')
or
SELECT TO_DATE((1900 + (LEFT(1180106, 3))) :: number || SUBSTR(1180106, 4, 4), 'YYYYMMDD')

SQLite Parsing TEXT column with date info

I'am using SQLite DB and I have "Date" column that is VARCHAR
I need to extract data between 2 dates...
this is what I tried....
SELECT * FROM Table1 WHERE Date BETWEEN '14/03/2017 17:00:10' AND '16/03/2018 17:00:12'
SELECT * FROM Table1 WHERE strftime('%d/%m/%y %H:%M:%S', Date) BETWEEN strftime('%d/%m/%y %H:%M:%S','15/07/2016 20:00:09') AND strftime('%d/%m/%y %H:%M:%S','16/07/2017 21:00:09')
SELECT * FROM Table1 WHERE strftime('%d/%m/%y %H:%M:%S', Date) BETWEEN '2017/07/15 20:00:09' AND '2017/07/17 21:00:09'
Any idea what I am doing wrong ?
If you have a date/time column, then just do:
SELECT t1.*
FROM Table1 t1
WHERE t1.Date >= '2017-03-14 17:00:10' AND
t1.Date < '2018-03-16 17:00:12';
Use ISO/ANSI standard date formats for constants!
I strongly discourage you from using between with date/time values. Here is a blog post on the subject, which although for SQL Server applies to all databases.
You can't use SQLite's strftime because it's formatting function, it can not parse input string.
Basically you have two options:
try to parse string using builtin functions
create user defined function
If you can rely on the fixed positions, you can easily parse your string and format it back to comply with one of supported SQLite DateTime formats. In your case it might look like this:
SELECT [Date] FROM Table1 WHERE
DATETIME(printf('%04d-%02d-%02d %02d:%02d:%02d',
substr([Date], 7, 4), substr([Date], 4, 2), substr([Date], 1, 2),
substr([Date], 12, 2), substr([Date], 15, 2), substr([Date], 18, 2)))
BETWEEN '2017-07-15 20:00:05' AND '2017-07-17 21:00:09'
Please note you have to change also syntax of BETWEEN to match one of supported DATETIME formats.

SQL Vertica, Cast Long issue

I have the following SQL statement
cast('long', ((substr(D.business_day, 1, 4)||substr(D.business_day, 6, 2))||substr(D.business_day, 9, 2))) AS bdate_id_yyyymmdd,
I have tried to change the 'long' to integer to see if that would work.
cast(integer, ((substr(D.business_day, 1, 4)||substr(D.business_day, 6, 2))||substr(D.business_day, 9, 2))) AS bdate_id_yyyymmdd,
Also tried integer(8)
getting error:
Error: [Vertica][VJDBC](4856) ERROR: Syntax error at or near ","
SQLState: 42601
ErrorCode: 4856
It's not only a syntax error. Vertica is relatively pecky at data types, too.
So you want to make an integer consisting of yyyymmdd from something that looks like an ISO date.
A string?
A date?
There are two ways to go about that. You can't SUBSTR() on a date; you can't call a function getting the day, month, or year, number, from an ISO formatted string.
This example illustrates what you can do. Remember, extracting integers from dates is faster than working with strings and substrings. If your business_day is a DATE type, do what I do with business_date below.
Oh, and Vertica also supports this syntax:
<expression>::INTEGER
to cast to integer.
Happy playing ...
Marco
WITH
d(business_date,business_day) AS (
SELECT DATE '2018-03-04', '2018-03-04'
UNION ALL SELECT DATE '1957-04-22', '1957-04-22'
UNION ALL SELECT DATE '1959-09-27', '1959-09-27'
)
SELECT
CAST(
substr(d.business_day, 1, 4)
||substr(d.business_day, 6, 2)
||substr(d.business_day, 9, 2)
AS int
) AS with_substr
, ( YEAR (business_date)*10000
+ MONTH(business_date)*100
+ DAY (business_date)
) AS with_calc
FROM d;
with_substr|with_calc
20,180,304|20,180,304
19,570,422|19,570,422
19,590,927|19,590,927

How to convert timestamp to date in SQL?

Image: http://i.stack.imgur.com/CyyHo.jpg
I have a dataset as shown in the image. I want to use the values of created_date column as datetime format. How can i do that?
The time you would like to convert is called Unix timestamp, which means how many seconds have passed since 1970/01/01. In this occasion you will have to use DATA function, which will take the total number of seconds and add them to 1970/01/01.
In SQL it is done in the following way:
select DATEADD(s, dateTimeInSeconds, '19700101')
Where s is Seconds and dateTimeInSeconds is your value of date; '19700101' is a starting point.
Column values does not seems to be a TimeStamp.
Use the below script to make the values as datetime .
SELECT credits,amount,city,
CONVERT(DATETIME,LEFT(created_date,8)
+' '+SUBSTRING(created_date,9,2)
+':'+SUBSTRING(created_date,11,2)
+':'+RIGHT(created_date,2)) Created_Date
FROM YourTable
Is created_date of type 'epoch'? And are you trying to use the datetime format within the SQL database or within your business logic? If it is in the business logic, it then depends on the language you use. I'd love to help but I don't fully understand the use case. If you could provide more information that would help us help you!
Cheers!
It doesn't look like unix timestamp but datetime without some chars (just digits are left).
To get "real" timestamp use in MySql: unix_timestamp()
To convert "real" timestamp to datetime use in MySql: FROM_UNIXTIME()
To convert your column to datetime use:
CONCAT(SUBSTR(created_date, 1, 4),
'-',
SUBSTR(created_date, 5, 2),
'-',
SUBSTR(created_date, 6, 2),
' ',
SUBSTR(created_date, 7, 2),
':',
SUBSTR(created_date, 11, 2),
':',
SUBSTR(created_date, 13, 2))