Computed column should result to string
You're probably defining your computed column as col1+col2
. Try CAST(col1 AS NVARCHAR(MAX))+'-'+CAST(col2 AS NVARCHAR(MAX))
instead.
Or if you prefer, you can replace NVARCHAR(MAX)
with NVARCHAR(10)
or a different length of your choice.
SQL computed column evaluating numbers should return string
You were close. Just a couple of syntax errors. Single quotes and END
ALTER TABLE dbo.test ADD classTest AS CASE
WHEN speed > 10 THEN 'fast'
ELSE 'error' END
Select * from test
speed classTest
11 fast
5 error
Build a string in a computed SQL column from specific characters from another column
This will produce YYMM
, like 2102
, 2103
, etc.:
SELECT CONVERT(char(4), GETDATE(), 12);
So we can add that to the existing computed column formula. Since BillingDateTo
is a computed column, you can't nest those; you have to repeat the computed column expression for BillingDateFrom
or, instead of using multiple computed columns, just expose these additional calculations in a view.
ALTER TABLE dbo.whatever ADD BillingDealerPeriodID
AS (CONVERT(char(4), DATEADD(DAY, 7, BillingDateFrom), 12));
- Example db<>fiddle
If you really want MMYY
instead of YYMM
, you can do:
SELECT STUFF(CONVERT(char(10), GETDATE(), 110),3,6,'');
So as a computed column:
ALTER TABLE dbo.whatever ADD BillingDealerPeriodID
AS STUFF(CONVERT(char(10), DATEADD(DAY, 7, BillingDateFrom), 110),3,6,'');
- Example db<>fiddle
The documentation doesn't do a great job of explicitly stating that computed columns can't be nested, other than the following:
The expression can be a noncomputed column name
But the error message that results if you try is pretty explicit.
As an aside, I strongly recommend staying away from variations that use FORMAT()
- while it's convenient, intuitive, and better matches capabilities in other more sophisticated languages, it is unacceptably slow (and also doesn't work in off-shoots like Azure SQL Edge).
Adding a calculated field to convert string value to date in T-SQL
The error on expiry_date
seems quite clear -- that is not the name of a column in the table. But you can simplify the logic:
ALTER TABLE TEM.AssignmentRates ADD Date_Expired AS
(TRY_CONVERT(date, Expiry_Date));
Actually, the nested SELECT
may have caused an issue. That would not normally be used for a computed column.
Create computed column with string in formula
This actually sorted it out for me at the end.....
NavigateUrl='<%# String.Format("{0}.aspx?ID={1}", DataBinder.Eval(Container.DataItem, "Category"), DataBinder.Eval(Container.DataItem, "Post_ID")) %>'
SQL Server: STRING_SPLIT() result in a computed column
At this time your answer is not possible.
The output rows might be in any order. The order is not guaranteed to
match the order of the substrings in the input string.
STRING_SPLIT reference
There is no way to guarantee which item was the third item in the list using string_split and the order may change without warning.
If you're willing to build your own, I'd recommend reading up on the work done by
Brent Ozar and Jeff Moden.
Is Custom Text supported in Computed columns?
Yes, as long as you stick with correct T-SQl expresions:
(cast([Duration] as varchar(...)) + 'MyCustomText')
SQL computed column based on a special character on another column
You would use a case
expression and like
:
select t.*,
(case when a like '%$%' then 'YES' else 'NO' end) as c
from t;
The following is just commentary.
This is very basic syntax for SQL. I would recommend that you spend some time to learn the basics. Learning-as-you-go is an okay approach -- assuming you have some fundamentals to build on. Otherwise, you are likely to spend a lot of time to learn a few things, and you may not learn the best way to do things.
Related Topics
Cannot Have a Qualifier in the Select List While Performing a Join W/ Using Keyword
SQL Count to Include Zero Values
Oracle SQL Developer "String Literal Too Long" Error
Linq to SQL "Not Like" Operator
Join Tables on Nearest Date in the Past, in MySQL
Select Single Row from Child Table for Each Row in Parent Table
Oracle Equivalent of Rowlock, Updlock, Readpast Query Hints
Creating a SQL Table from a Xls (Excel) File
Xquery - How to Use the SQL:Variable in 'Value()' Function
How to Cancel a SQL Server Execution Process Programmatically
How to Make a Stored Procedure Return a "Dataset" Using a Parameter I Pass
Parsing Openxml with Multiple Elements of the Same Name
Correct Way to Take a Exclusive Lock
Oracle Pl/SQL Results into One String
Transpose Rows to Columns Based on Id Column
How to Display Column Headers Returned by Postgresql Function