Combine concat with substr in a sql query - sql

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

Related

Change nonth from 8 to 08 in SQL query

I am trying to get the month year from date that is in database.
I used the following query to get the date. But the formatting is not as I require. Due to this my query is not giving the result which I am expecting.
select top 3 datepart(yyyy, cia.DateCreated) as YYyy, datepart(mm, cia.DateCreated) as mm,
case when (datepart(mm, cia.DateCreated) < 10) then
cast(concat('0',datepart(mm, cia.DateCreated)) as varchar)
else
datepart(mm, cia.DateCreated)
end as Months
from v_AuthListInfo cia
I am getting the month values as 1, 2, 3, 4, 5, 6, 7, 8 but I need the value like 01, 02, 03, 04 ...
I tried to use DatePart, Cast and Concatinate function but still I am getting the data like 1, 2, 3, 4.
use right() function
RIGHT('0' + RTRIM(MONTH(cia.DateCreated)), 2);
You can use the built-in function format():
format(month(cia.DateCreated), '00')
If you want to use the right() method, I would suggest:
right(concat('00', month(cia.DateCreated)), 2)
Here's general approach how to pad with ceratin character:
right(#padding + cast(#stringToPad as varchar), 2)
where #padding consists of characters you want to use and its length has to be the desired length, so in your case it would be 00.
#stringToPad is string you want to extend, in your case cast(month(cia.DateCreated) as varchar).
So finally, giving an example, it would look like:
select right('00' + cast(1 as varchar), 2)

Unexpected error converting text to date in SQL Server

I have a problem converting int values of year, month and day into date value.
SELECT datefromparts(b.Install_year, b.shipm_month, b.shipm_day)
I have already found a solution to the problem, discovering that some values of DAY were 0. But I am curious why things work certain way.
SELECT datefromparts(2005, 4, b.shipm_day)
For testing purpose I set year and month values fixed. Putting there 1,3,5,7,8,10,12 as month and running that in Management Studio I can see the results. Values : 2,4,6,9, and 11 cause the error below:
"Cannot construct data type date, some of the arguments have values which are not valid."
Anyone has idea why certain values work and other don't?
This would presumably be because the "day" value is 31 and those months do not have 31 days.
datefromparts() only constructs valid dates.
This is not that easily fixed because datefromparts() doesn't have a "try_" version.
Here is one method:
select coalesce(try_convert(date, concat(2005, '-', 4, '-', b.shipm_day)),
try_convert(date, concat(2005, '-', 4, '-', b.shipm_day - 1)),
try_convert(date, concat(2005, '-', 4, '-', b.shipm_day - 2)),
try_convert(date, concat(2005, '-', 4, '-', b.shipm_day - 3))
)

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')

Format date information from string with 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')

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