I have a table called documents one of the fields is called location which shows the file path for the document. I need to change it from D:\........ to H:\.....
How can I do this using update in sql as the file paths vary in length and there are lots of records
You can use string helper function to achieve the same. Something like below
UPDATE documents SET location = 'H:' + Mid(location, 2, Len(location) - 2)
WHERE Left(location, 1) = 'D'
Here, Len() function returns the length of the string literal
Left() function returns 1 character from the left of the string literal
Mid() function give you substring from a string (starting at any position)
See MS Access: Functions for more information on the same.
Related
I want to use LIKE operator in access 10 sql query with variable.
Example:
temporary variable var contains value bs
var = "bs"
I want to match every String that starts with value of temporary variable followed by zero or more numbers.
I am trying to fire the query:
select * from xyz where variety LIKE "#[tempvars]![var] + [0-9]*"
It is returning 0 records.
Thankz for the help.
You need to refer to your tempvar outside of the quotes, and use & for concatenation:
select * from xyz where variety LIKE "#" & [tempvars]![var] & "[0-9]*"
This will return all records where variety starts with a literal #, then whatever is in [tempvars]![var], then a number, and then any amount of characters.
You can check if that variety is available in your table or not. If that variety is available in your table then don't search with like operator and otherwise use like operator.
I am trying to extract the last file name from a field in SQL where the separator is /, and there is also one after the last file name. (I am using this to create a new filed in a BI web intelligence document.)
Filename1/filename2/filename3/filename4/ result required Filename4
File1/file2/file3/file4/file5/file6 result required file6
I have tried various combinations but without success. As you can see the file names are not of a standard length and the number of folders is variable.
Any help on this would really be appreciated.
Thank you
Lyn
Depending on your answer to my comment ... do you have a input string that ends with "/" or not ? I have put both types of test strings in this query using SQL 2008 as dbms. Just comment out either Set #tstString to run each condition and you will see the two result possibilities.
Declare #tmpFirstMark int
Declare #tmpLastMark int
Declare #tmpUseMark int
Declare #tstString varchar(100)
Set #tstString = 'Filename1/filename2/filename2/filename4/'
Set #tstString = 'File1/file2/file3/file4/file5/file6'
-- Calculate 1st Occurrence of "/"
Set #tmpFirstMark = PATINDEX('%/%',#tstString)
-- Calculate last Occurrence of "/"
Set #tmpLastMark = (LEN(#tstString) - PATINDEX('%/%',REVERSE(#tstString)) + 1)
-- Calculate 2nd to last Occurrence of "/"
Set #tmpUseMark = #tmpLastMark - PATINDEX('%/%', REVERSE(SUBSTRING(#tstString, 1, #tmpLastMark-1)))
Select
#tstString
,#tmpFirstMark
,#tmpLastMark
,#tmpUseMark
,SUBSTRING(#tstString, #tmpLastMark + 1, LEN(#tstString)) as 'resultSTR'
,SUBSTRING(#tstString, #tmpUseMark + 1, #tmpLastMark-#tmpUseMark-1) as 'otherResult'
I would use a regular expression to retrieve the desired output :
([^/]+)/?$
This will match as many non-/ characters as possible (at least 1) before the end of the string, that may be followed by an optional /.
You will want to use the first group of the match to retrieve the filename of a directory without its trailing /.
You haven't specified your RDBMS and I'm not so confortable with using regexps in SQL so I hope you'll be able to piece that together in your SQL dialect.
I have a product table called PDPRODP - for certain styles within this table I used a concat statement to add a full-stop to their description (PRDESC), I now wish to remove this full stop.
The descriptions are varying length, the field max size is 30 characters and I need to physically remove the full-stop rather than using a select statement to trim the full-stop.
I tried;
UPDATE PDPRODP SET PRDESC = PRDESC-1 where PRSTYLE = 1234
But I got this error:
Character in CAST argument not valid.
I also tried this following some googling;
UPDATE PDPRODP SET PRDESC=LEFT(PRDESC, LEN(PRDESC)-1)
WHERE PRCOMP = 1 AND PRSTYL = 31285
But got this error:
LEN in *LIBL type *N not found.
Use LENGTH
UPDATE PDPRODP SET PRDESC=LEFT(PRDESC, LENGTH(PRDESC)-1)
WHERE PRCOMP = 1 AND PRSTYL = 31285
The REPLACE() function can search for all occurrences of some string, and substitute another in its place. You might search for your full-stop, and replace it with a zero-length string ''. This would be handy in cases where your search string may not always be at the end.
I've been working on this for days and can't seem to work it out. Basically I need return digits from a field before there is a forward slash. e.g. if the field was 1234/TEXT I want to return 1234. I can't just use left fieldname 4 as the digits vary in left e.g. 12345/TEXT, so it needs to be anything left of the forward slash. Now in the World of MS Access, it is something like this - and it works
Left(TABLE!FIELD,InStr(1,TABLE!FIELD,"/")-1)
However, how do I convert this to be used in an IBM\DB2 system? The DB2 SQL seems somewhat different to 'normal' SQL.
Thanks!
Rather than INSTR, maybe LOCATE
LOCATE(char, string)
char is the search term
string is the string being searched
You can achieve this by combining LOCATE with SUBSTR;
Locate information
Substring information
Cheat sheet (for this example);
SUBSTRING('FIELD','START POSITION', 'LENGTH')
LOCATE('SEARCH STRING', 'SOURCE STRING')
SUBSTRING lets you retrieve specific characters from a string, i.e.;
AFIELD = 'Hello'
SUBSTRING(AFIELD,4,2)
Result = 'lo' (position 4 and 5 of Hello)
LOCATE returns the position of the first character of the search string it finds as a number, i.e.;
AFIELD = 'Hello'
LOCATE('ello', AFIELD)
Result = 2 (it starts at position 2)
So you can combine these to do what you want, example;
XTABLE has 1 column called ACOL with the following values in it;
123467/ABCD
1321/ABDD
1123467/ABCD
To just retrieve the numbers;
SELECT SUBSTRING(ACOL,1, LOCATE('/',ACOL)-1)
FROM XRDK/XTABLE
Result;
123467
1321
1123467
What are we doing?
SUBSTRING(
ACOL,
1,
LOCATE('/',ACOL)-1
)
SUBSTRING(
Field ACOL,
Starting at position 1,
Length; using locate set this to where I find a '/' and subtract 1 from the
resulting postion (without the -1 you'd have the / on the end)
)
Try this
SELECT SUBSTRING(CAST (ROUND(COLUMN,2) AS DECIMAL(6,2)), 0, locate('/',CAST (ROUND(COLUMN,2) AS DECIMAL(6,2))))
FROM TABLE
How do I determine if a SQL Server 2012 field contains a specific character or string without using fulltext commands using a select?
You could use the CHARINDEX() function if you just wanted to see if the string or character exists in the input rather than selecting the whole row:
DECLARE #string VARCHAR(50)
SET #string = 'string to search'
SELECT CHARINDEX('to',#string)
This code would return 8 (the start position of the string you searched for), if you searched for something that didn't exist, it would return 0, so you could determine if a string existed by looking for a return integer greater than 0.