I am working on TableA that has Column1 with dates in this format: YYYY-MM-DD and Column2 with numbers range from 1-12.
I am trying to change the date year (THE YEAR ONLY) to 2022 WHERE Column2 = 10
NOTE: I am not trying to change the months and date.
Data type is date.
UPDATE TableA
SET Column1 = '2021'
WHERE Column2 = 10
If you are storing your dates as date data type, then you could do it mathematically as the following:
For SQL server:
update TableA
set Column1 = dateadd(year, -(year(Column1) - 2022), Column1)
where Column2 = 10
Demo
For MySQL
update TableA
set Column1 = date_add(Column1, interval -(year(Column1) - 2022) year)
where Column2 = 10
Demo
Ideally you would never store a date as a string - use the correct datatype i.e. date. Also I assume YYY-MM-DD is a typo and that it should read YYYY-MM-DD.
Then a simple string concatenation should work
create table TestTable (Column1 varchar(10), Column2 int);
insert into TestTable (Column1, Column2)
select '2021-12-10', 10;
update TestTable set
Column1 = CONCAT('2022', SUBSTRING(Column1,5,6))
where Column2 = 10;
select * from TestTable;
Returns:
Column1
Column2
2022-12-10
10
Note: Works on both SQL Server and MySQL.
UPDATE TableA
SET Column1 = '2022' + substring(Column1,6,5)
WHERE Column2 = 10
So it is hard replacing 2022 to the first 4 characters and appending the rest of column1 to the end
Related
Q1. Why doesn't my conversion work in the below trials?
Q2. How to convert varchar to bigint and return results only greater than 90 from a varchar column?
I am on Microsoft SQL Server 2014 Management Studio.
I know I can retrieve numbers by simply:
SELECT column1 FROM table WHERE ISNUMERIC(column1) = 1
but now I want to add to it and return results only greater than 90. But before I can do that I keep getting this error when trying to convert. I am trying to convert varchar to bigint because I have some really big numbers in the varchar column. Here is the error --> Error converting data type varchar to bigint.
for example I have these values in the column
50
65
98
199
9999999
9999999999
Expected results
98
199
9999999
9999999999
Tried queries
Trial 1
SELECT
CAST(P0.column1 as bigint) as column1
FROM
(
SELECT column1 FROM table WHERE ISNUMERIC(column1) = 1
) P0
Trial 2
select case when isnumeric(column1 ) = 1
then cast(column1 as bigint)
else NULL
end
from table
Trial 3
select *
into TempTable
FROM table WHERE Cast(ISNUMERIC(column1) as bigint) = 1 and column1> '90';
Trial 4
Insert into TempTable (column1, column2, column3, column4, column5)
select column1, column2, column3, column4, Cast(ISNUMERIC(column5) as
bigint)
from table;
Trial 5
select cast(Param_Value as bigint) from Setting_Alert_Value;
SQL Server does not execute the WHERE clause before other expressions, necessarily. Subqueries and CTEs -- same lack of guarantee. SQL Server can rearrange the elements of a query, sometimes causing problems like this.
In addition, ISNUMERIC() doesn't guarantee that something can be converted to an integer.
Just use TRY_CONVERT() or TRY_CAST() instead:
SELECT TRY_CAST(column1 as bigint) as column1
FROM table
here is the exact query that worked for me
SELECT TRY_CAST(column1 as bigint) as column1 FROM table Where TRY_CAST (column1 as bigint) > 90 order by column1;
Declare #temp As Int
set #temp = 10
select Column1 from MyTable where Column2 = #temp
select Column1 from MyTable where #temp = Column2
Is there any change in performance based on position of parameter in where clause.
There should be no change. However, You can confirm this by running the execution plan in sql viewer.
No, those two should be identical.
I want to get row from based on parameter value. Either it could be value 'ABC' or NULL. Below is the source table and expected result, which I'm trying to achieve.
SourceTable
column1 column2
--------------------------
value1 NULL
value2 ABC
Tried with query, but it is getting two rows which are with value1 and value2.
Declare #Param1 varchar(20) = 'ABC'
Select *
from SourceTable
where column2 = #Param1 Or column2 is NULL
If value is 'ABC' then Result -
column1 column2
--------------------------
value2 ABC
If value is NULL then Result -
column1 column2
--------------------------
value1 NULL
Perhaps this would work for you?
select *
from SourceTable
where column2 = #Param1 or (#Param1 is null and column2 is null)
You can try something like: Only problem you might encounter with this is if your column2 has blanks.
SELECT *
FROM SourceTable
WHERE ISNULL(column2, '') = ISNULL(#Param1, '')
Perhaps you want union all and a check for existence:
Select *
from SourceTable
where column2 = #Param1
union all
Select *
from SourceTable
where column2 is null and not exists (select 1 from sourcetable st2 where st2.column2 = #Param1);
An alternative uses order by -- if you want only one row:
select top 1 st.*
from sourcetable st
where column2 = #param1 or column2 is null
order by (case when column2 is not null then 1 else 2 end);
I have the following query:
IF EXISTS ( SELECT Column1 ,
Column2
FROM dbo.Table1
EXCEPT
SELECT Column1 ,
Column2
FROM #proposedData )
BEGIN
SELECT Column1 ,
Column2
FROM dbo.Table1
EXCEPT
SELECT Column1 ,
Column2
FROM #proposedData
RAISERROR('Unexpected values in proposed data',16,10)
END
I want to raise an error if the data that already exists in a table does not appear in a table variable. This is to make sure that my source code matches what is in a particular environment. The problem with this is that the two select queries are duplicate code. This introduces a possibility for human error - the two queries should be the same but could be different. An alternative is:
SELECT Column1 ,
Column2
FROM dbo.Table1
EXCEPT
SELECT Column1 ,
Column2
FROM #proposedData
IF ##ROWCOUNT <> 0
BEGIN
RAISERROR ('Invalid ObjectType values',16,10)
END
However this 'pollutes' the query output as there will be an empty result set if the data is correct. So, is there way to prevent a result set being output if there are 0 rows? An idea is a stored proc that takes the select and constructs the first example query from it but was wondering there was a built in way to do it.
Thanks
Joe
How about this ?
if exists(SELECT Column1 ,
Column2
FROM dbo.Table1
EXCEPT
SELECT Column1 ,
Column2
FROM #proposedData)
RAISERROR ('Invalid ObjectType values',16,10)
I have the following columns in TableA
TableA
Column1 varchar
Column2 int
Column3 bit
I am using this statement
IF Column3 = 0
SELECT Column1, Column2 FROM
TableA WHERE
Column2 > 200
ELSE
SELECT Column1, Column2 FROM
TableA WHERE
Column2 < 200
But the statment does not compile. It says Invalid Column Name 'Column3'
Column3 is not being referenced anywhere outside of the IF and ELSE blocks. If you wish to reference this value you will need to declare a new variable and use that;
DECLARE #btColumn3 BIT
SELECT #btColumn3 = Column3 FROM #tblTableA
IF #btColumn3 = 0
SELECT Column1, Column2 FROM
#tblTableA WHERE
Column2 > 200
ELSE
SELECT Column1, Column2 FROM
#tblTableA WHERE
Column2 < 200
Or do the following;
IF (SELECT Column3 FROM #tblTableA) = 0
SELECT Column1, Column2 FROM
#tblTableA WHERE
Column2 > 200
ELSE
SELECT Column1, Column2 FROM
#tblTableA WHERE
Column2 < 200
Either way you will have to ensure that the query used to retrieve Column3 returns a single result either by limiting your query so that it can only return a single value or using MIN(), MAX() etc depending on your requirements.
Also, if you need to execute more than one query within the IF and ELSE blocks you will need to wrap the contents in BEGIN and END as follows:
IF #btColumn3 = 0
BEGIN
// Do a query
// Do another
END
ELSE
BEGIN
// Do a query
// Do another
END
If you want to do this you need to first store the value of Column3 in a variable.
Declare #temp money
Select #Temp = Column3
From TableA
IF #Temp = 0
begin
SELECT Column1, Column2 FROM
TableA WHERE
Column2 > 200
end
ELSE
begin
SELECT Column1, Column2 FROM
TableA WHERE
Column2 < 200
end
Obviously, this assumes that there will only be one value returned for Column3.
EDIT:
This is a different approach which I think should work for you:
declare #CutOffValue money
declare #MaxValue money
Set #CutOffValue = 200
Set #MaxValue = 9999999999
Select Column1, Column2
From TableA
Where Column2 > Case When Column3 = 0 Then #CutOffValue Else 0 End
And Column2 < Case When Column3 = 0 Then #MaxValue Else #CutOffValue End
You are mixing 2 different levels:
IF is at the TSQL (procedure) level and cannot depend on the row values
SELECT is the query itself where the row values can be used to filter the result set
The following would work
IF Condition /* independent of the different values of TableA. can be an aggregate though */
BEGIN
SELECT Column1, Column2 FROM
TableA WHERE
Column2 > 200
END
ELSE
BEGIN
SELECT Column1, Column2 FROM
TableA WHERE
Column2 < 200
END
You will need the syntax as follows
IF <CONDITION>
BEGIN
<Your Statement>
END
ELSE
<Your Statement>
Hope this is helpful!!
Assuming you have posted the complete query, then the problem with your IF clause is that you are assuming that it can use the columns from the SELECT statement following it. It cannot and does not. This is why it will not compile.
You need your test condition to be separate from the statements following the IF clause. See on MSDN.
DECLARE #test BIT
SELECT #test = 0
IF #test = 0
BEGIN
SELECT Column1, Column2 FROM
TableA WHERE
Column2 > 200
END
ELSE
BEGIN
SELECT Column1, Column2 FROM
TableA WHERE
Column2 < 200
END
Why not just do
select Column1, Column2 from TableA where
Column2 > 200 and Column3 = 0 or Column2 < 200 and Column3 = 1
or, abusing arithmetics,
select Column1, Column2 from TableA where (Column2 - 200) * (2 * Column3 - 1) < 0
Since the answer to your problem varies largely on exactly what logic has to be implemented, analyzing present scenario all I can give you is a small and compact query which can meet your requirements (I hope so…)
SELECT Column1,column2 FROM TableA WHERE
(CASE WHEN Column3=0 then Column2 else 2)>(CASE WHEN Column3=0 then 200 ELSE 1)
AND (CASE WHEN Column3<>0 then Column2 else 1)<(CASE WHEN Column3<>0 then Column2 else 2)