Erik Martinez Barraza
Erik Martinez Barraza

Reputation: 41

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

Upvotes: 0

Views: 712

Answers (1)

HoneyBadger
HoneyBadger

Reputation: 15140

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

Upvotes: 1

Related Questions