Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Table data is
1-"A11111,B22222,C33333,D44444,B22222"
2-"A11111,C33333,D44444,B22222,D44444"
3-"A11111,E55555,C33333,D44444,B22222"
If will use
select *
from table
where data = 'E55555'
can get number 3 data
Thanks
And yet another variant
SELECT * FROM [table] WHERE ','+[data]+',' LIKE '%,E55555,%'
The problem though, is that what you are asking for won't be able to use normal indexes, which means an inefficient table scan every time you query. You may want to consider breaking the comma delimited values into their own columns so you can query on them efficiently. As it is you have a 'super' column that crams multiple pieces of data into a single column.
Try this:
SELECT * FROM [table] WHERE [data] LIKE '%E55555%'
This query will help you to handle comma separated data search:
SELECT * FROM [table] WHERE
[data] ='E55555' OR -- IF record contains only this value
[data] LIKE 'E55555,%' OR -- If record starts with the value followed by comma and others
[data] LIKE '%,E55555' OR -- If record ends with the value preceeded by comma and others
[data] LIKE '%,E55555,%' -- If record contains the value in middle
Refer SQL Fiddle here
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 21 hours ago.
This post was edited and submitted for review 10 hours ago.
Improve this question
dear gurus, SQL.
Please advise how to do the following:
You need to take a date from one column and join it with the time of another column,
and then add one day to the received date.
I wrote such a query, but on some data it gives a conversion error.
Please tell me a more optimal query so that it always works when combining the date and time format into datetime.
Thank you.
create table dt (date1 datetime, date2 datetime)
insert into dt values('19000101 17:17:00.000','19070101 17:51:00.000')
insert into dt values('19000101 18:20:00.000','19080101 18:21:00.000')
insert into dt values('20000101 06:00:00.000','20100101 06:40:00.000')
select
dateadd(dd,1,convert(datetime,convert(date,date1))+ convert(datetime,convert(time, date2)))
from dt
Here is an example where such an error could occur:
CREATE TABLE Example (
Duration TIME,
AdditionalDelay SMALLINT
)
INSERT INTO dbo.Example (Duration, AdditionalDelay)
VALUES ('00:01:02',3)
SELECT Duration+AdditionalDelay FROM dbo.Example
To fix this, you need to identify the proper unit of measure for AdditionalDelay and use DATEADD instead of +. For example:
SELECT DATEADD(MINUTE,AdditionalDelay,Duration) FROM dbo.Example
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have data streaming from an external program to TABLE1 in my SQL Server database. Let's say TABLE1 consists of columns data1 and data2. How do I update to another table (TABLE2) when a value changes in TABLE1?
For the sake of an example, let's say a value in the data1 column changes.
Note: I wrote a trigger that works, but this is only on the basis of a manual update. I can't figure out how to automate this, meaning the trigger would compare new and old values from the table on its own and then perform the update if a value is different.
Something like below would work,
CREATE TRIGGER after_update_table1
AFTER UPDATE ON table1
FOR EACH ROW
BEGIN
IF OLD.val1<> new.val1 THEN
/**
-- do operations on table2
**/
END IF;
END;
-- if val1 allows null we need to check by adding more conditions to the if clause like,
IF((OLD.val1 IS NULL AND NEW.val1 IS NOT NULL) OR (OLD.val1 IS NOT NULL AND NEW.val1 IS NULL) OR (OLD.val1<>NEW.val1)) THEN
/**
-- operations on table2
**/
END IF;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In sql server, when I create a column like:
select *, '1' as test
what is the type of this new column i.e 1 ?
Just one more question, can I reference this column on other table?
For SQL Server, you can answer such questions easily using sp_describe_first_result_set:
exec sp_describe_first_result_set N'select *, ''1'' as test from sys.objects'
Produces a result set, the last row of which indicates that the test column in that result set is of type varchar(1). (Other results in this case, pulling the other columns from the sys.objects table make it clear that this procedure is capable of describing columns as non-var char(2) or nvarchar(128), so it's not just a display issue)
This column has char(1) type and yes u can use this column in joins or other manipulation etc as well.
because its value is a string within ' ' If you want it to be an integer. you can do the following:
select *, Cast(1 as INT) as test FROM SomeTable
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a database with a bunch of tables.
I want to build a query, that lists all the tables in the database, and then do various counts in those tables.
For example count "How many rows that has 'D' as country" etc.
I have figured out to do a SELECT name FROM db.sys.tables to get all the tables, just can't wrap my head around the situation that I want to do counts on the tables also.
Its the same count I want to do in all tables.
You can use the undocumented system procedure sp_msforeachtable like this
DECLARE #results TABLE(tbl VARCHAR(100),countresult INT);
INSERT INTO #results
EXEC sp_msforeachtable #command1 = 'SELECT ''?'',Count(*) FROM ? /* Add your WHERE clause here*/',#replacechar = '?'
SELECT * FROM #results
You can read more about it here and here
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am creating a new SQL table which will have a date column, which will contain only Month and Year (eg: November 2014). My concern is that I will need to match this column in a query to do a JOIN. A relevant extract of that query is shown below:
(
SELECT
ReservationStayID,
datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar) as [MTH],
count(*) AS [Nights Spent],
avg(RateAmount) as [Rate],
min(CreatedOn) as CreatedOn,
min(StayDate) as [DateOfArrival],
max(StayDate) as [DateOfDeparture]
FROM ReservationStayDate
GROUP BY ReservationStayID, datename(m,StayDate) + ' ' + cast(datepart(yyyy,StayDate) as varchar)
) x ON x.ReservationStayID = b.ReservationStayID
The date column of my new table will be matched with the output from line 3 of the query above (that is, datename(m,StayDate.....as [MTH])
I just want to get it right in my new table so that I don't mess up things later when doing JOINS,etc.
I've read that I can use smalldate, or split the Month and Year as Varchar in 2 columns. So, what would you recommend?
I recommend you to use one datetime column
if you are sure that you will never ever use the whole date you should go with two separate fields for month and year but you do must have those values split both sides of the join otherwise there will be no advantage going this path: month name will be a varchar/nvarchar column but year should be an integer as it will always be a number.
one of the advantages of two separate columns is that you don't have to make the math at every join so you must have these values separate both sides of the join.
what i mean is that you have to modify the structure of table ReservationStayDate also, adding month and year as sparate values; the easiest solution is to create two calculated columns.
if you can use persisted calculated columns you could also have indexes on these columns and that will likely improve performances when joining & searching.
THE advantage i see: if you need (and use only) the information 'month' and 'year' it is cumbersome and costly to translate a 'date' information wherever you need 'month' and 'year' in the whole application. you can make classes, helper methods and so on but if you store the 'clean' information in the database it is easier to use and maintain it at all levels.