I have the following vba code that I am converting to SQL...
Do While Not sRst1.EOF
sRst1.MoveLast
sRst1.MoveFirst
If (sRst1!Field1 = "FileName") Then
Exit Do
End If
sCtr = sCtr + 1
sRst1.MoveNext
Loop
So basically I need to grab the first record where field1 is equal to FileName, which I could probably do by selecting TOP(1) where field1 = "FileName" if I'm not mistaken? Also, I need to return a count of how many records it took to get to that record. How could I do that?
WITH t AS (SELECT *,ROW_NUMBER() OVER(ORDER BY (SELECT 0)) row_num FROM sRst1)
SELECT field1,row_num FROM t WHERE field1 = 'FileName'
As FrankPl points out, row_num will be effectively random if you don't tell SQL what order you want. Replace "(SELECT 0)" with "field1" or some other column to fix this.
Related
I have a DB where one column is a _text type which means an ARRAY of TEXT.
I need to update every row in this column to trim the white spaces for every element in that array.
I know how to do a single text in every row
UPDATE
sites
SET
site_id = TRIM(site_id);
but I don't know how to do it for an ARRAY of TEXT
I found a way to SELECT but I need to UPDATE
SELECT
array_agg(trim(e))
FROM (
SELECT
row_number() OVER () AS rn,
unnest(attached_sites) e
FROM
institutional_review_boards) t
GROUP BY
rn;
No clue how to do it for the _text type in PostgreSQL
You can go on with the current query except for using row_number() function such as
UPDATE institutional_review_boards AS i0
SET attached_sites = (SELECT ARRAY_AGG(TRIM(e))
FROM (SELECT UNNEST(attached_sites) e
FROM institutional_review_boards AS i1
WHERE i1.site_id = i0.site_id) AS i)
Issue: I have a query in T-SQL which I have ROW_NUMBER as RowNumber set and Partitioned on a column called UnitNumber in my SELECT. I want to create a CASE statement that says:
CASE
WHEN RowNumber = 1 then un.SqFt Else ''
End as GrpSqft
However, I am receiving an error that my RowNumber column is invalid. Am I placing this in the wrong location? I am a rookie to Sql and am having trouble understanding how to define the column as a "built-in" column. I get:
[ Invalid column name 'RowNumber'. ]
Desired Result is to have a column "GrpSqft" with all SqFt values that fall into RowNumber=1
Thank you,
Needs to be
CASE
WHEN ROW_NUMBER() OVER (PARTITION BY UnitNumber ORDER BY BLAH DESC) = 1 THEN un.sqft ELSE ''
END AS GrpSqft
This question is a little hard to word, so I'll show you what I need
CASE
WHEN t1.RANGE_START = t1.RANGE_END
Then (select RANGE_START
from .dbo.t1
)
END as Value
what I'm trying to do is make it so that if range start and range end are equal then just use the range start as the value for each row.
I understand that what the query is doing right now is trying to select every Range_start for each value where the range_start = range_end so how do I limit the sub query to only pull that value for the current row?
Kind of guessing without trying I am afraid but you can't be far away...something like this.
SELECT
CASE
WHEN t1.RANGE_START = t1.RANGE_END
THEN t1.RANGE_START
ELSE t1.RANGE_END
END AS Value,
column1,
column2,
column3
FROM dbo.t1
I usually find this resource pretty useful
I have a varchar column that contains data like '0 03-03-14', '1 04-03-14' and so on.
I need to select the maximum one, in that case '1 04-03-14'.
My problem is that I can have the '1' (the max value) but how do I have to do if I also want the date ?
For now, I have this :
SELECT MAX(TO_NUMBER(SUBSTR(revision, 1, INSTR(revision, ' ')-1)))
FROM table
WHERE name = 'aname'
AND t_name = 'tname'
GROUP BY revision
Does anyone have an idea ?
Thanks
I assume you want something like max(version, date version), try this:
Select * from (
Select * from (
SELECT
TO_NUMBER(SUBSTR(revision, 1, INSTR(revision, ' ')-1)) as rev
, TO_DATE(SUBSTR(revision, 1, INSTR(revision, ' ')+1),'DD-MM-YY') as revDate
FROM table
WHERE name = 'aname'
AND t_name = 'tname'
) extracted
order by extracted.revDate, extracted.rev desc
) where rownum = 1
Do you mean this?
select max(revision)
from table
where name = 'aname' and t_name = 'tname';
It seems strange to put the revision number and date in one column. Is that really your data format? If not, modify your question with the actual data format.
Regarding your answers, I think the best solution would be to split those two parts :)
Using Sql Server 2005
Table1
ID Value
ABC001
BCE002
...
I have search column, when i search the id like this "001" it is showing empty, when I search "ABC001" then it is showing the values.
Query
Select * from table where id = '" & txtid & "'
txtid = textbox
I want to search only the numeric value in the ID.
How to write a query for the numeric search.
Need Query Help
select * from MyTable
where Value LIKE '[A-Z][A-Z][A-Z]001'
OR
select * from MyTable
where Value LIKE '[A-Z][A-Z][A-Z]' + RIGHT('000' + CONVERT(VARCHAR, #id), 3)
SELECT * FROM Table1 WHERE id LIKE '%001'
The answer given by Mitch Wheat will work if every ID you give is 3 letters in caps followed by a numberical value which has 3 digits.
The answer given by rayman86 essentially disregards anything in the front. It merely looks for anything that ends with 001.
If the length of the ID varies and/or the first few characters are not even letters, it's better to use the code that rayman86 has provided. So just use the SELECT * FROM Table WHERE ID LIKE '%Numbers' where Numbers is your numerical value.
A literal interpretation of the question without making any assumptions:
Create this function
create function dbo.numericonly(#s varchar(max))
returns float as
begin
declare #i int set #i = 1
while #i <= len(#s)
if ascii(substring(#s,#i,1)) between 48 and 57
set #i = #i + 1
else
set #s = stuff(#s,#i,1,'')
return #s
end
GO
Use this query
Select * from table1 where dbo.numericonly(id) = '" & txtid & "'