My SQL table have a column name AddressName . I am storing customer address name in that column. And I want to store them in the following format, suppose when I inserted first time a value in that column it stored 'Utpal' in that field next time before inserting any value I want to fetch the next string value to be inserted as 'Utpal2' and will insert that value in the table.Similarly in the next insertion the value should be 'Utpal3'.How to do this task using SQL query please help.
Thanks and Regards
Utpal Maity
Something like this would help you. Just an example:
TABLE SCHEMA:
create table person
(
id int identity(1,1),
address varchar(50)
)
GO
SCRIPT: (partial)
--insert second record
insert into person
select top 1 left(address, case patindex('%[0-9]%', address) when 0 then len(address) else patindex('%[0-9]%', address) - 1 end) +
convert(varchar(10),convert(numeric(10,0),Coalesce(NULLIF(right(address, case patindex('%[0-9]%', address)
when 0 then 0
else len(address) - patindex('%[0-9]%', address) + 1
end
)
,'')
,0)
) + 1
)from person
order by id desc
GO
Check out SqlFiddle:
Related
Following Situation:
I have a dynamically (by columns) builded table ... eg:
Rowid UniqueID Name Birthdate Town ....
1 null Joe Jan-93 Cologne
2 null Nick Okt-00 London
I am building this TempTable to create an uniqueID for all Data in my DataBase
The TempTable was created by two loops which run through all my DataBase Table & Columns and copy all primary key Data to this TempTable.
My aim is to update the UniqueID Column from my TempTable with the concat values of data ... eg:
UniqueID
JoeJan-93Cologne
NickOkt-00London
Do you have an idea how to update UniqueID ?
What I m thinking about is:
Loop 1 going through all Tables
Select Table of Schema
Loop 2 going through all Columns of Table
Select Column of Schema
Copy Column to my Temp
-- here an update like ... set UniqueID = select concat(UniqueID, #Column)
-- from #table where RowID = RowID
End loop 2
end loop 1
Is this possible
Or do I have to open a third loop which is running through all rows and concat values ?
You can try this
Update <YourTableName>
set UniqueId = ISNULL(Name, '') + ISNULL(Cast(Birthdate as Varchar(10), '') + ISNULL(Town, '')
You can use CONCAT() with UPDATE statement, no any loop required :
UPDATE t
SET UniqueID = CONCAT(Name, Birthdate, Town);
I use CASE statement in SQL to check the value for Grade column. If user entered 7 for example I stick leading 0 in front so value will be saved as 07. Everything worked fine until we added K grade as an option in our app. Now if user enter K value will be saved as 0K. Here is example of my SQL Case statement:
CASE
WHEN LEN(LTRIM(RTRIM(GRADE))) = 0 THEN NULL
ELSE RIGHT('00'+ISNULL(LTRIM(RTRIM(GRADE)),''),2)
END
Code above is used in INSERT SELECT statement. I'm wondering if I can prevent leading zero when user enter K grade? Is that doable with modifying existing CASE statement? I have tried adding this:
WHEN GRADE = 'K' THEN 'K'
But this did not fix the problem, my INSERT statement failed. If anyone can help or knows better solution please let me know. Thank you!
This is the way I would do it -
IF EXISTS(SELECT TOP 1 1 FROM sys.tables WHERE name = 'tb_Test')
BEGIN
DROP TABLE dbo.tb_Test
END
CREATE TABLE dbo.tb_Test (ID INT IDENTITY(1,1) PRIMARY KEY, Grade NVARCHAR(10))
INSERT dbo.tb_Test (Grade)
VALUES ('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9'),('10'),('K')
SELECT CASE WHEN LEN(Grade) = 1 AND ISNUMERIC(Grade) = 1 THEN '0'+Grade
WHEN ISNUMERIC(Grade) = 1 THEN Grade
WHEN ISNUMERIC(Grade) = 0 THEN Grade END
FROM dbo.tb_Test
Here is one way to do it:
Create and populate sample table (Please save us this step in your future questions)
DECLARE #T AS TABLE
(
GRADE varchar(3)
)
INSERT INTO #T VALUES
('7'), (''),('03'),('001'),('K'), (NULL)
The query:
SELECT CASE WHEN LEN(LTRIM(RTRIM(GRADE))) = 0 THEN NULL
WHEN GRADE NOT LIKE '%[^0-9]%' THEN RIGHT('00' + GRADE, 3)
ELSE GRADE
END As Res
FROM #T
Results:
Res
007
NULL
003
001
K
NULL
See a live demo on rextester.
I have an old SQL database (Microsoft's SQL Server) with thousands of rows that contains data as follows:
ID urlString
1 page.aspx?pageID=34
2 page.aspx?pageID=163
3 page.aspx
4 page.aspx?pageID=23
I've added a new column (pageID) to the database. I want to create an UPDATE query to copy the pageID from the URLstring and insert it in the new column (pageID) as follows. If there is no pageID I want to add 0. How can I accomplish that?
ID URLstring pageID
1 page.aspx?pageID=34 34
2 page.aspx?pageID=163 163
3 page.aspx 0
4 page.aspx?pageID=23 23
UPDATE YourTable
SET pageID=
SUBSTRING(urlString,CHARINDEX('=', urlString)+1,CHARINDEX('=', urlString))
To have the 0 value
UPDATE YourTable
SET pageID=
CASE
WHEN CHARINDEX('=', urlString) > 0 THEN
SUBSTRING(urlString,CHARINDEX('=', urlString)+1,CHARINDEX('=', urlString))
ELSE 0
END
please try the following
SELECT urlString, cast(REVERSE(SUBSTRING(REVERSE(urlString),0,CHARINDEX('=',REVERSE(urlString)))) as smallint) AS [pageID]
Hope this helps.
Well, whichever SQL language variant the database uses will make a difference here. (i.e. SQLplus, NOSQL, etc.) However, it shouldn't be toooooooooo complicated. Assuming that both columns are INTs, you could probably just do something like so
UPDATE table_name
SET pageID = URLstring;
Here is some other sources for additional information.
A similar stack overflow question:
Copy data from one column to other column (which is in a different table)
This is a TutorialsPoint webpage on the matter: http://www.tutorialspoint.com/sql/sql-update-query.htm (TutorialsPoint is usually one of my first stops for any programming-related knowledge, it's quite a valuable website.)
This is a w3schools webpage on the matter: http://www.w3schools.com/sql/sql_update.asp
Hope you get everything figured out!
You can achieve this by the PARSENAME using the following UPDATE query:
UPDATE TestTable
SET pageID = CASE WHEN ISNUMERIC(PARSENAME(REPLACE(URLstring, '=', '.'), 1)) = 1 THEN PARSENAME(REPLACE(URLstring, '=', '.'), 1)
ELSE 0 END
Sample execution with the given sample data
DECLARE #TestTable TABLE (ID INT, URLstring VARCHAR (200), PageID INT);
INSERT INTO #TestTable (ID, URLstring, pageID) VALUES
(1, 'page.aspx?pageID=34' , NULL ),
(2, 'page.aspx?pageID=163' , NULL ),
(3, 'page.aspx' , NULL ),
(4, 'page.aspx?pageID=23' , NULL );
UPDATE #TestTable
SET pageID = CASE WHEN ISNUMERIC(PARSENAME(REPLACE(URLstring, '=', '.'), 1)) = 1 THEN PARSENAME(REPLACE(URLstring, '=', '.'), 1)
ELSE 0 END
So the SELECT * FROM #TestTable will result as:
ID URLstring PageID
------------------------------------
1 page.aspx?pageID=34 34
2 page.aspx?pageID=163 163
3 page.aspx 0
4 page.aspx?pageID=23 23
I try it:
update yourtable
set pageID=
case when
substring(URLstring, charindex('=', URLstring) +1, len(URLstring) - charindex('=', URLstring) )
=URLstring then '0'
else
substring(URLstring, charindex('=', URLstring) +1, len(URLstring) - charindex('=', URLstring) )
end
Using CASE, PATINDEX and SUBSTRING it's fairly easy to extract the page id from the url.
Create and populate sample table (Please save us this step in your future questions)
DECLARE #T As TABLE
(
Id int IDENTITY(1,1),
URLString varchar (40),
PageId int
)
INSERT INTO #T (URLString) VALUES
('page.aspx?blabla=yadayada&pageID=34'),
('page.aspx?pageID=163'),
('page.aspx'),
('page.aspx?pageID=23')
Update statement:
UPDATE #T
SET PageId = CAST(
CASE WHEN PATINDEX('%pageID=%', URLString) > 0 THEN
SUBSTRING(URLString, PATINDEX('%pageID=%', URLString) + 7, LEN(URLString))
ELSE
'0'
END
As int)
Verification:
SELECT Id, URLString, PageId
FROM #T
Results:
Id URLString PageId
----------- ---------------------------------------- -----------
1 page.aspx?blabla=yadayada&pageID=34 34
2 page.aspx?pageID=163 163
3 page.aspx 0
4 page.aspx?pageID=23 23
I have a table of Users and if the is companyID that is input is equal to the companyID the User their name is shown and all the other names are shown as XXXXX. I need a way to set it to "Name"(literally the word name) and then an incremented number for how ever many there are.
select
case when t1.CompanyID = #id then t1.name else
(
'XXXXX'
)
end as Name
with input 148 shows
i want to show this, and so on for more names(name3, name4, etc..):
With foo being the name of your table, then this works (see sqlfiddle) :
SET #id = 142;
SET #i = 0;
select
companyID,
CASE WHEN foo.CompanyID = #id
THEN foo.name
ELSE (Concat('Name',#i := #i+1))
END as Name
FROM foo;
Try this please.
select [CompanyID],
case when([CompanyID]=#Id) then 'Name' +
convert(nvarchar,ROW_NUMBER() OVER(ORDER BY Name DESC)) else Name end as Name
from [dbo].[Table_1]
order by [CompanyID]
Can somebody help me with this little task? What I need is a stored procedure that can find duplicate letters (in a row) in a string from a table "a" and after that make a new table "b" with just the id of the string that has a duplicate letter.
Something like this:
Table A
ID Name
1 Matt
2 Daave
3 Toom
4 Mike
5 Eddie
And from that table I can see that Daave, Toom, Eddie have duplicate letters in a row and I would like to make a new table and list their ID's only. Something like:
Table B
ID
2
3
5
Only 2,3,5 because that is the ID of the string that has duplicate letters in their names.
I hope this is understandable and would be very grateful for any help.
In your answer with stored procedure, you have 2 mistakes, one is missing space between column name and LIKE clause, second is missing single quotes around search parameter.
I first create user-defined scalar function which return 1 if string contains duplicate letters:
EDITED
CREATE FUNCTION FindDuplicateLetters
(
#String NVARCHAR(50)
)
RETURNS BIT
AS
BEGIN
DECLARE #Result BIT = 0
DECLARE #Counter INT = 1
WHILE (#Counter <= LEN(#String) - 1)
BEGIN
IF(ASCII((SELECT SUBSTRING(#String, #Counter, 1))) = ASCII((SELECT SUBSTRING(#String, #Counter + 1, 1))))
BEGIN
SET #Result = 1
BREAK
END
SET #Counter = #Counter + 1
END
RETURN #Result
END
GO
After function was created, just call it from simple SELECT query like following:
SELECT
*
FROM
(SELECT
*,
dbo.FindDuplicateLetters(ColumnName) AS Duplicates
FROM TableName) AS a
WHERE a.Duplicates = 1
With this combination, you will get just rows that has duplicate letters.
In any version of SQL, you can do this with a brute force approach:
select *
from t
where t.name like '%aa%' or
t.name like '%bb%' or
. . .
t.name like '%zz%'
If you have a case sensitive collation, then use:
where lower(t.name) like '%aa%' or
. . .
Here's one way.
First create a table of numbers
CREATE TABLE dbo.Numbers
(
number INT PRIMARY KEY
);
INSERT INTO dbo.Numbers
SELECT number
FROM master..spt_values
WHERE type = 'P'
AND number > 0;
Then with that in place you can use
SELECT *
FROM TableA
WHERE EXISTS (SELECT *
FROM dbo.Numbers
WHERE number < LEN(Name)
AND SUBSTRING(Name, number, 1) = SUBSTRING(Name, number + 1, 1))
Though this is an old post it's worth posting a solution that will be faster than a brute force approach or one that uses a scalar udf (which generally drag down performance). Using NGrams8K this is rather simple.
--sample data
declare #table table (id int identity primary key, [name] varchar(20));
insert #table([name]) values ('Mattaa'),('Daave'),('Toom'),('Mike'),('Eddie');
-- solution #1
select id
from #table
cross apply dbo.NGrams8k([name],1)
where charindex(replicate(token,2), [name]) > 0
group by id;
-- solution #2 (SQL 2012+ solution using LAG)
select id
from
(
select id, token, prevToken = lag(token,1) over (partition by id order by position)
from #table
cross apply dbo.NGrams8k([name],1)
) prep
where token = prevToken
group by id; -- optional id you want to remove possible duplicates.
another burte force way:
select *
from t
where t.name ~ '(.)\1';