Click here to Skip to main content
15,851,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SELECT product_id, 
SUBSTRING (product_id FROM 0 FOR 3) AS First_Part,
SUBSTRING (product_id FROM 5 FOR 2) AS Second_Part,
SUBSTRING (product_id FROM 8 FOR 8) AS Last_Part
FROM product;


What I have tried:

I used this query to get the product ID in 3 parts.

But only in first row I am not getting expected results, but the data in rest of the rows are correct.

I am using PGADMIN

can someone check and please help me.
Posted
Updated 15-May-23 10:18am
Comments
OriginalGriff 14-May-23 4:18am    
Without any idea what your data looks like, what you are getting as a result, and what you expect to get, we can't help you.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

Use the "Improve question" widget to edit your question and provide better information.
Member 15627495 14-May-23 7:11am    
substring first char is at 1 for start index.
SELECT product_id, 
SUBSTRING(product_id,1,3) AS 'First_Part',
SUBSTRING(product_id,6,2) AS 'Second_Part',
SUBSTRING(product_id,9,9) AS 'Last_Part'
FROM product


by this query, the result will be 4 columns.
when you're stuck, or on a faulty query, step your query build, it's to debug slowly.
by starting at beginning, executing the query as its length increase.

Someone can say : " delete all your query, write the first column only select, execute the query, and check if it's well wwritten and if the result is good.
after this, make the query with two columns, check the result .. until you have the await Result.
Wendelius 14-May-23 11:52am    
Try posting the original data in the table and an example what the query should return. Without this info there's not much we can do

1 solution

Accordingly to the documentation (PostgreSQL: Documentation: 9.1: String Functions and Operators[^]):
SQL
substring('Thomas' from 2 for 3)
--returns: hom


Conclusion: your query is incorrect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900