Alternate in Access2007 [closed] - sql-server

This question already has answers here:
Upserting in MS-access
(6 answers)
Closed 7 years ago.
I want to check whether a row exists or not before inserting a new one in Access 2007.
I have the following working query in SQL Server but I'm having trouble converting it to Access.
update category set name='hell' where categoryid=287
if ##rowcount=0
begin
insert into category(categoryid,name,path,parentcategoryid,creationdate) values (287,'a','a',12,'')
end

Try this
update category set name='hell' where categoryid=287;
if not exists(select * from Category where categoryid=287)
insert into category(categoryid,name,path,parentcategoryid,creationdate)
values (287,'a','a',12,'');

Related

Calculate Date Difference in SQL Server 2019 [duplicate]

This question already has answers here:
Possible to store value of one select column and use it for the next one?
(4 answers)
How to use a calculated column to calculate another column in the same query using a subquery
(2 answers)
SQL Server Reference a Calculated Column
(6 answers)
Using CASE on a Calculated Column in SQL Server 2012
(2 answers)
Closed 6 months ago.
I am trying to calculate the date difference and keep getting a column name error. I am new to SQL and learning from books and YouTube. Any assistance would be appreciated. I commented out the code not working
declare #rpDT datetime
set #rpDT = getdate()
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,isnull([visDischargeDT],#rpDT)as disDT
,datediff(day,[visAdmitDT],disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
SQL Server and most of the RDBMS do not allow to reference column at the same level. The closest equivalent to achieve similar effect is CROSS APPLY:
declare #rpDT datetime = getdate();
SELECT [OrgKey]
,[visID]
,[visPatID]
,[visInternal]
,[visName]
,[visAssignedNS]
,[visAssignedRoom]
,[visAssignedBed]
,[visAdmitDT]
,s.disDT
,datediff(day,[visAdmitDT],s.disDT) as Pt_days
FROM [MH_Pharmacy_Hub].[MC].[dbo_Visits]
CROSS APPLY (SELECT COALESCE([visDischargeDT],#rpDT)) AS s(disDT);

Get everything after and before certain character in SQL [duplicate]

This question already has answers here:
A SQL Query to select a string between two known strings
(18 answers)
Closed 2 years ago.
I have a string like
https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100
I want everything after the equal sign = and before &, i.e the number 5 in SQL Server
Try this below logic using SUBSTRING and CHARINDEX:
Demo Here
DECLARE #T VARCHAR(200) = 'https:\/\/www.abc.ca\/v3\/homes\/190135717\/responses\/bulk?page=5&per_page=100'
SELECT SUBSTRING(#T,CHARINDEX('=',#T,0)+1,CHARINDEX('&',#T,0)-CHARINDEX('=',#T,0)-1)

Passing variable in Group By in SQL Server [duplicate]

This question already has answers here:
I need to pass column names using variable in select statement in Store Procedure but i cannot use dynamic query
(3 answers)
Where can parameters be used in SQL statements?
(2 answers)
Using SQL parameters to DROP INDEX in C#
(2 answers)
Closed 5 years ago.
How can I pass variable in Group BY expression.Here is my code.
and from #Varientsku I want to pass column name
Create Proc spIsUnique
#varientsku nvarchar(max) output
As
Begin
select #varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
#varientsku
End.
and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
You need dynamic sql
EXEC ('select '+#varientsku+',
IIf (count(*)>1,''Duplicate'',''True'') as Total
from product
group by '+#varientsku)
Note : Make sure #varientsku is sanitized, this code is vulnerable to sql injection

Update Query for string replace in sql server [duplicate]

This question already has answers here:
How to replace a string in a SQL Server Table Column
(10 answers)
Closed 8 years ago.
i have table,
id Name
1 aaaaAAbbbb
2 bbbbAAaaaa
3 aabbAAbbaa
This is my table,i want to update 'AA' with 'BB' using sql server 2008
update myTable set [Name] = REPLACE([Name], 'AA', 'BB')
REPLACE applies to SQL Server 2008 upwards.
You can use
REPLACE(Name,'AA','BB');
and update you column.

Transact-SQL using variable for table name [duplicate]

This question already has answers here:
A table name as a variable
(10 answers)
Closed 9 years ago.
I have try to use statement below in my SQL. Is it possible by any means to dynamically select table form database. (For example select table name from Comobox and then display data from selected table)
SET #var1 = 'test';
SELECT * From #var1
Dynamic SQL requires that you create a string consisting of the sql statement, and then you execute the string. Dynamic t-sql quotes in string

Resources