Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a column of nText data type and having the values with in and out characters respectively. Need to get the count of in's and out's.
Example: 1st row contains 9:00:in(A111),11:00:out(A111),.......
And 2nd row so on....

What I have tried:

Tried with Casting (to nvarchar).
Posted
Updated 7-Nov-20 22:14pm
Comments
Richard Deeming 2-Nov-20 9:32am    
ntext, text, and image (Transact-SQL) - SQL Server | Microsoft Docs[^]
IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

These types have been deprecated since 2005. How old is the application you're working on?!
Jörgen Andersson 3-Nov-20 2:56am    
What database and version is it?

1 solution

That is a very poor design. Each in/out should be a separate row with a separate column for the date.

However, to answer your question..
Calculate the LENGTH of the data, and the length of the data after REPLACE 'in' with ''. Subtract one from the other, then adjust for the number of chars you replaced.

E.g. Data is '09:00:in(A111),11:00:out(A111),12:00:in(A111),13:00:out(A111)'
Length of data is 61
Replace 'in' gives '09:00:(A111),11:00:out(A111),12:00:(A111),13:00:out(A111)'
Length of that is 57
61 - 57 = 4
We replaced 2 chars so divide by 2 = 2 = the number of times 'in' was in the original data
For 'out' we get 61 - 55 = 6
Divide by 3 (length of 'out') to get the 2 times 'out' appeared in the original data

Edit: if you insist on keeping the ntext column type (don't - use DDL to change the column type, don't try to 'cast' it) you will need the 'datalength' function not the len() function.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900