Skip to content

Oracle SQL hierarchical query from bottom to top

I have a table where I want to go from bottom to top using hierarchical queries. The problem is that I need the get the value of one column from root (top) using CONNECT_BY_ROOT, but since I reverse the way the hierarchical query works (reverse the prior in connect by and the start with), this function (CONNE…

How to extract date from a long string in SQL Server

I a long string like below in my column and I need to extract only the date (2021-07-05) from it. Could anyone please help? Answer We can use PATINDEX with SUBSTRING here: Demo The call to PATINDEX above finds the starting position of the date, while SUBSTRING takes 10 characters from that position.

Unable to convert string to Timestamp

I’m trying to convert a sample 12 hour date with AM/PM into a timestamp, however, Snowflake is throwing an error that it’s not parsable: SELECT TO_TIMESTAMP(‘7/16/2021 4:52:25 AM’, ‘MM/dd/yyyy HH12:mm:ss AM’) The error message returned: Can’t parse ‘7/16/2021 4:…

Finding the first empty column in a data row

I saw similar questions and most turned into arguments about table design, normalization, etc. Not all of us have the luxury of making our clients change their database designs. Here is my dilemma. My client asks that I make it possible for their workers to be able to add call out time for the preceding day. …

SQL – Replace characters in a SQL string

I would like to know how to replace all characters between 2 dashes (-) and replace the dashes as well. It should be in a single select statement, not a loop. The text between the dashes can be variable length and any character. The characters before and after the dashes can also be variable length. Answer Yo…