Skip to main content
added 31 characters in body
Source Link
Vérace
  • 30.6k
  • 9
  • 71
  • 84

I think you want something like this:

SELECT 
  v.visit_id, 
  v.tvp_id, 
  LEFT(date_range, 4)::INT, 
  RIGHT(date_range, 4)::INT
FROM tvp.visit v 
WHERE  date_format = 'DMY' AND date_range NOT LIKE 'Pre%';

To answer your question I did the following:

Created a table:

CREATE TABLE my_range(the_range VARCHAR(9)); -- just the field of interest!

Some sample values:

INSERT INTO my_range VALUES ('1969-1986');
INSERT INTO my_range VALUES ('1932-1987');
INSERT INTO my_range VALUES ('1956-1999');

Query:

SELECT LEFT(the_range, 4)::INT, RIGHT(the_range, 4)::INT FROM my_range;

Result:

Left Right
1969 1986
1932 1987
1956 1999

See the dbfiddle here.

You can check that they are now integers by running:

SELECT LEFT(the_range, 4)::INT + 20, RIGHT(the_range, 4)::INT - 20 FROM my_range;

And you can see that addition isand subtraction are indeed performed on the new values in the fiddle.

I think you want something like this:

SELECT 
  v.visit_id, 
  v.tvp_id, 
  LEFT(date_range, 4)::INT, 
  RIGHT(date_range, 4)::INT
FROM tvp.visit v 
WHERE  date_format = 'DMY' AND date_range NOT LIKE 'Pre%';

To answer your question I did the following:

Created a table:

CREATE TABLE my_range(the_range VARCHAR(9)); -- just the field of interest!

Some sample values:

INSERT INTO my_range VALUES ('1969-1986');
INSERT INTO my_range VALUES ('1932-1987');
INSERT INTO my_range VALUES ('1956-1999');

Query:

SELECT LEFT(the_range, 4)::INT, RIGHT(the_range, 4)::INT FROM my_range;

Result:

Left Right
1969 1986
1932 1987
1956 1999

See the dbfiddle here.

You can check that they are now integers by running:

SELECT LEFT(the_range, 4)::INT + 20, RIGHT(the_range, 4)::INT - 20 FROM my_range;

And you can see that addition is indeed performed on the new values.

I think you want something like this:

SELECT 
  v.visit_id, 
  v.tvp_id, 
  LEFT(date_range, 4)::INT, 
  RIGHT(date_range, 4)::INT
FROM tvp.visit v 
WHERE  date_format = 'DMY' AND date_range NOT LIKE 'Pre%';

To answer your question I did the following:

Created a table:

CREATE TABLE my_range(the_range VARCHAR(9)); -- just the field of interest!

Some sample values:

INSERT INTO my_range VALUES ('1969-1986');
INSERT INTO my_range VALUES ('1932-1987');
INSERT INTO my_range VALUES ('1956-1999');

Query:

SELECT LEFT(the_range, 4)::INT, RIGHT(the_range, 4)::INT FROM my_range;

Result:

Left Right
1969 1986
1932 1987
1956 1999

See the dbfiddle here.

You can check that they are now integers by running:

SELECT LEFT(the_range, 4)::INT + 20, RIGHT(the_range, 4)::INT - 20 FROM my_range;

And you can see that addition and subtraction are indeed performed on the new values in the fiddle.

Source Link
Vérace
  • 30.6k
  • 9
  • 71
  • 84

I think you want something like this:

SELECT 
  v.visit_id, 
  v.tvp_id, 
  LEFT(date_range, 4)::INT, 
  RIGHT(date_range, 4)::INT
FROM tvp.visit v 
WHERE  date_format = 'DMY' AND date_range NOT LIKE 'Pre%';

To answer your question I did the following:

Created a table:

CREATE TABLE my_range(the_range VARCHAR(9)); -- just the field of interest!

Some sample values:

INSERT INTO my_range VALUES ('1969-1986');
INSERT INTO my_range VALUES ('1932-1987');
INSERT INTO my_range VALUES ('1956-1999');

Query:

SELECT LEFT(the_range, 4)::INT, RIGHT(the_range, 4)::INT FROM my_range;

Result:

Left Right
1969 1986
1932 1987
1956 1999

See the dbfiddle here.

You can check that they are now integers by running:

SELECT LEFT(the_range, 4)::INT + 20, RIGHT(the_range, 4)::INT - 20 FROM my_range;

And you can see that addition is indeed performed on the new values.