I have a string column where I need to find the substring until the first '-' is found.
Example column ELOT-IGS-2. I need to get "2" as output.
These columns come from a table so I cannot declare the variable as a fixed string.
I tried LOCATE, SUBSTRING_INDEX but none are build in functions.
I also tried RIGHT(ID_BETSLIP,CHARINDEX('-',ID_BETSLIP)-1) but this does not work when I have 2 times "-"
Does anyone have an idea?
select RIGHT(<your Field>, CHARINDEX('-',REVERSE(<your Field>))-1)
DECLARE #t varchar(20) = 'ELOT-IGS-2'
select REVERSE(SUBSTRING(reverse(#t),0,CHARINDEX ('-',REVERSE(#t))))
Simplest would be
select SUBSTRING(reverse(ELOT-IGS-2),0,charindex('-',reverse(ELOT-IGS-2)))
Based on #mohan111's solution you can add REVERSE in order to get what you need
DECLARE #t varchar(20) = 'ELOT-IGS-2'
select REVERSE(SUBSTRING(reverse(#t),0,CHARINDEX ('-',REVERSE(#t))))
You should have done this yourself, once you got #mohan111's solution! For your own improvement you can not ask for everything :-( Once you got a solution that is almost what you need, it is time to try to improve it yourself. Please check this MSDN page, and every time that you need to parse a string, START HERE :-) Try to go over the functions and maybe you will find the solution.
The REVERSE query and most string parsing function are not good solution in SQL Server, and in most cases you are better to do this using SQLCLR function.
Related
I have a system that has a number of canned reports and we are looking to further customize those reports. Looking at the XML, I see the following SELECT statement.
SELECT #GetProperty("VATFlag","0",false)# AS CONDITION
FROM LOCATION_ORGANIZATION
WHERE organizationID #organizationid#
Now, I see that #organizationid is a variable required in the report, and I can pass something to it. However, I don't know what the dual # in #organization# is, also, why the strings within SELECT and AS CONDITION are and what it would return.
Can someone enlighten me?
How would I take apart a column that contains string:
92873-987dsfkj80-2002-04-11
20392-208kj48384-2008-01-04
Data would look like this:
Filename Yes/No Key
Abidabo Yes 92873-987dsfkj80-2002-04-11
Bibiboo No 20392-208kj48384-2008-01-04
Want it to look like this:
Filename Yes/No Key
Abidabo Yes 92873-987dsfkj80-20020411
Bibiboo No 20392-208kj48384-20080104
whereby I would like to concat the dates in the end as 20020411 and 20080104. From the right side, the information is the same always. From the left it is not, otherwise I could have concatenated it. It is not an import issue.
As mentioned in the comments already, storing data like this is a bad idea. However, you can obtain the dates from those strings by using a RIGHT function like so:
SELECT RIGHT('20392-208kj48384-2008-01-04', 10)
Output:
2008-01-04
Depending on the SQLSERVER version you are using, you can use STRING_SPLIT which requieres COMPATIBILITY_LEVEL 130. You can also build your own User Defined Function to split the contents of a field and manipulate it as you need, you can find some useful examples of SPLIT functions in this thread:
Split function equivalent in T-SQL?
Assuming I'm correct and the date part is always on the right side of the string, you can simply use RIGHT and CAST to get the date (assuming, again, that the date is represented as yyyy-mm-dd):
SELECT CAST(RIGHT(YourColumn, 10) As Date)
FROM YourTable
However, Panagiotis is correct in his comment - You shouldn't store data like that. Each column in the database should hold only a single point of data, be it string, number or date.
Update following your comment and the updated question:
SELECT LEFT(YourColumn, LEN(YourColumn) - 10) + REPLACE(RIGHT(YourColumn, 10), '-', '')
FROM YourTable
will return the desired results.
I have to truncate the first couple of digits out of a Guid on a table. Is it possible to do it only using a SQL script? Or I have to do it programatically?
Thanks!
To answer the direct question at hand (and assume the column's name is foo):
foo is uniqueidentifier:
SELECT substring(convert(nvarchar(50), foo), 3)
foo is simply a string:
SELECT substring(foo, 3)
3 is just an arbitrary starting offset to remove the first "few" characters.
With that said, this sounds like of like an XY problem. If you're running into an issue where you feel you need to truncate the first few characters, it would be important to list that information in your question as well as what you've described sounds like an odd request. However, you're also entitled to have odd requests.
The previous answer was perfectly good. Another option is to use the wonderful RIGHT function. Assuming the Guid is a a uniqueidentifier, then it has 36 characters in it. Just use RIGHT(theguid, 34), e.g.
declare #temp as uniqueidentifier = newid();
select right(#temp, 34);
I am a newbie to both stackoverflow and SQL, so I do hope for your understanding ! Recently I have faced many SQL issues and I do hope some help could be provided to aid me. This is because I have tried sourcing for information, some did answer to my problems, but gave me more problems after that.
Firstly, I have seen a recent post by user javascriptstress in his replacement of values in a column. In my issue, I was working on credit card details in SQL whereby I had to censor the digits on the user's credit card values, which will show a result of xxxxxxxxxxxx2345 instead of '2734948533562345' for Visa card. However by using SUBSTRING and LEN I am unable to get the values hidden. Due to the fact that not all credit cards have 16 digits, I have faced the problem of only providing the last 4 digits of the credit card number. It sure is possible if i code each credit card number one by one, but what if I have to hide the first 12 numbers for many credit cards? Is there a more convenient way of settling this ?
Help is greatly appreciated!
Ps. I'm kind of new to technology, I'm sorry for any problems caused :(
If you're querying:
SELECT REPLICATE('x', LEN(ccnumber) - 4) + RIGHT(ccnumber, 4))
FROM ...
REPLICATE repeats the string n times.
RIGHT takes the n rightmost characters from the string.
For SQL 2012 onward I'd recommend the safer CONCAT to assemble the string:
SELECT
CONCAT(
REPLICATE('x', LEN(ccnumber) - 4),
RIGHT(ccnumber, 4))
FROM ...
CONCAT concatenates two strings; the + operator also does this but CONCAT will never treat the values as numbers and try to add them.
You can dynamically determine where to cut the string based on it's length, then use REPLICATE to pad by the correct amount.
DECLARE #cc VARCHAR(16) = '1234123412341234'
DECLARE #len INT
SELECT #len = LEN(#cc)
DECLARE #ss VARCHAR(4)
SELECT #ss = SUBSTRING(#cc, LEN(#cc)-3, 4)
SELECT REPLICATE('X', #len-4) + #ss
Output:
XXXXXXXXXXXX1234
This is obviously not a PCI-compliant way of dealing with credit card data.
You could chain these statements together to use them in a larger SELECT statement. You could even define a scalar valued function that lets you call it without the hassle.
A slightly cleaner version of this (without the variable declarations I put in for sake of example):
SELECT REPLICATE('X', LEN(#cc)-4) + SUBSTRING(#cc, LEN(#cc)-3, 4)
But I would suggest using Mr. Gibbs version as it using the RIGHT function to simplify it even more.
How can i get the index of a point which is in my LineString?
I am using SQL Server 2008 R2.
I do have a geography type where a LineString is saved in.
I want now to get the index of two points on this LineString. So that I know which one occurs first.
Is this somehow possible?
Because right now i'm doing it for my self with a while loop... but it's really slow when i've got some more data in my database :/
EDIT: Ok, right now i'm trying to follow the solution from SQL to use CHARINDEX.
Some more background info:
I do have a geo point, I do have a linestring. I did get the intersecting points with a radius around my point from the linestring. Ok, now i want to try to get with the first intersecting point the index from this point on the LineString.
So i do have in my linestring some numbers like these patterns "1.123456 or 12.123456 or 123.123456" and my search point is also something like "1.123456 or 12.123456 or 123.123456"
The Problem is, that STIntersection gives me some different numbers back which are variable at the fractional digits. I thought about some string formatting, but i don't know how i should solve this. If there would be some nice regex features i think it would make my life easyier :)
I had a look through all of these functions but couldn't find anything for my needs.
Maybe some more experienced people could help me with that.
Thanks!
In case the datatype is varchar used. Please see below.
You can use CharIndex
DECLARE #document varchar(64)
SELECT #document = 'abcdef12345wuerzelchen'
SELECT CHARINDEX('abc', #document)
Once you have got the first occurrence point, Now you can check for another.
Declare #position int
Set #position = CHARINDEX('abc', #document)
SELECT CHARINDEX('wuerzelchen', #document, #position)
For more information you can check here