55

I need to convert String ( text ) to UUID ( Postgres ) and keep the same sorting like for a String. Is it possible? I saw the UUID base on the time, so maybe it's not possible?

4 Answers 4

81

In PostgresSQL's SQL grammar Using

concat(UUID,'')

returns a text result. Using

uuid(text)

returns a UUID result.

Sign up to request clarification or add additional context in comments.

1 Comment

- concat(uuid1, s2) works, uuid(str) - Unknown database function 'uuid'
26

In PostgreSQL, apart from using uuid(), it's also possible to specify the type explicitly like ::uuid:

with myconst (__ef_filter__id_0, __filter_workitemid_0, __filter_projectid_1, __id_2) as (
values ( 'fcb8284c-1bd4-4d50-b5df-09a091b01d8c'::uuid, '9e4b70a7-c222-47dd-87cb-fbbaaf396ccd'::uuid, uuid('2b10c0a5-e35d-425d-a71a-9e473924ac4c'), uuid('3fa85f64-5717-4562-b3fc-2c963f66afa6')) )
select ...

Comments

3

There is a class in JDK dedicated to the management of UUIDs, called java.util.UUID. There's a static method fromString in it that should fit your goal. As far as I can see, you can use instances of UUID in JDBC insert statements.

Comments

2

At oVirt open source project, we use the PostgreSQL uuid type to store our Primary keys and Foreign keys.
We have build a wrapper called Guid that uses the java.util.UUID class to hold the read data from the DB.
When retrieving a ResultSet (we use spring-jdbc) we use the getString method in order to get the UUID value as String, and then use the fromString method of java.util.UUID.
You can git clone our project and look at ovirt-engine/backend/manager/modules/dal (our data access layer) project for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.