In my DB, I store various version numbers, like the following :
OBJNAME
Fix_6.0.0a.1
Fix_6.0.0a.2
I would like to sort them not according to last version element (the number behind the last . character. How do I write such SQL statement ?
I guess it's something like:
SELECT SUBSTR(INSTR(OBJNAME, ".", -1)) as LAST_VERSION, OBJNAME
FROM MY_TABLE
ORDER BY LAST_VERSION
But what is the exact syntax?
The correct version is
select TO_NUMBER(SUBSTR(OBJNAME,INSTR(OBJNAME,'.',-1)+1,LENGTH(OBJNAME))) as LAST_VERSION, OBJNAME from MY_TABLE order by LAST_VERSION
i dont know which sql-software you are using, but you should have a look at substr and instr parameters. in you case you are passing 3 parameters to instr and 1 parameter in substr. but substr normally requires more. insert some blanks to get an overview of your statement, especially for the substr.... as LAST_VERSION. then you will see. wrong param count.
Related
using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder
I am trying to automate the data input of a adress data base however some of the housenumber have a letter added to the end of them. Because my database keeps the letter and the number separate i need to somehow split these 2 up. I know that i need a substring for this. My question is however how do i get the location of the first letter.
for example 17b
would give me 3
where 259a
would give me 4
You should consider using REGEXP_INSTR or REGEXP_SUBSTR as the standard INSTR and SUBSTR functions won't have the kind of flexibility that you need.
In your case, you would use something like REGEXP_INSTR(column_name, '[^0-9 ]').
Use regexp_replace for both numbers or letters
select regexp_replace('259a', '[A-Za-z]') from dual;
select regexp_replace('259a', '[0-9]') from dual;
FIDDLE
I am working with an Oracle database and would like to write a REGEXP_LIKE expression that finds any number where all digits are the same, such as '999999999' or '777777777' without specifying the length of the field. Also, I would like it to be able to identify characters as well, such as 'aaaaa'.
I was able to get it working when specifying the field length, by using this:
select * from table1
where regexp_like (field1, '^([0-9a-z])\1\1\1\1\1\1\1\1');
But I would like it to be able to do this for any field length.
If a field contains '7777771', for example, I would not want to see it in the results.
Try this:
^([0-9a-z])\1+$
Live demo
You're almost there. You just need to anchor the end of the regex.
^([0-9a-z])\1+$
I am using Oracle 11 G and have the following set of data:
12.0
4.2
Version.1
7.9
abc.72
I want to return all string characters before the period. What sort of query would I run in order to achieve this? Any help would be greatly appreciated, thanks!
You can try a combination of instr and substr.
Something like this:
select substr(field, 1, instr(field, '.') - 1)
from your_table;
Assuming field always contains a . character on it.
You can also deal with strings without a . by using case, if or any other similar valid conditional function on Oracle's SQL language implementation.
Of course, you can always put this on a function to make it look nicer on your query.
I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.