Update Table with Columns - sql

I have a table with the following columns, City, State, Zip, and Zone.
I want to update Zone with City - State - Zip with hyphens.
Is there a way to do this in SQL without grabbing all the records and iterating through the results and doing an update statement for each one?

You should be able to do the following if I am not mistaken:
UPDATE [Table_Name] Set Zone = (City + '-' + State + '-' + Zip)

update table set zone = city + '-' + state + '-
+ Zip
Note if the datatypes are different you might need to do something like
update table set zone = cast(city as varchar(50)) + '-' + cast(state as varchar(50)) + '-
+ cast(Zip as varchar(50))

Related

Pipe delimited output

I have the below code that I need the format in pipe delimited format. I tried adding +'|' but seems I am not putting that correctly so it keeps on throwing errors. It can't be a stored procedure because of the rights. Also, I cannot change the result output permanently so can't use the tools-options menu. Could someone please add the pipe correctly for me in the below select statement. Thanks a ton. I am so confused.
SELECT DISTINCT
Name,
CAST(DOB AS DATE) AS DateOfBirth,
Address1 AS AddressStreet1,
Address2 AS AddressStreet2,
City AS AddressCity,
State AS AddressState,
Postal AS AddressZip,
Sex AS Gender,
CAST(ActivityDateTime AS DATE) AS ActivityDate,
CASE
WHEN ID IN ('R','A')
THEN 'Y'
ELSE 'N'
END AS Allow
I'm really not sure if this is what you're looking for, but one option is to throw all of the fields into one with a pipe between them. The UNION ALL allows you to display the headers in the first row.
SELECT 'Name|DateOfBirth|AddressStreet1|AddressStreet2|AddressCity|AddressState|AddressZip|Gender|ActivityDate|Allow' AS Data
UNION ALL
SELECT DISTINCT
Name + '|' +
CAST(DOB AS DATE) + '|' +
Address1 + '|' +
Address2 + '|' +
City + '|' +
State + '|' +
Postal + '|' +
Sex + '|' +
CAST(ActivityDateTime AS DATE) + '|' +
CASE
WHEN ID IN ('R','A')
THEN 'Y'
ELSE 'N'
END
AS Data

Add selected values from multi column into one column separated by ','

I want to select values from multiple columns into one column. I have 2 separate columns from which i want to get name,address,state,zip in following format in SQL Server 2008
Name (new line)
address,state,zip
Name (new line)
address,state,zip
Query:
select
name + char(13) + concat(address,',', state,',', zip)
from
tbl1
join
tbl2 on....
I am not able to get the desired output. I get concat is not a recognized built in function name.
You could use + operator and cast the zip field as varchar directly like this:
For example:
select 'Dara Singh' + char(13) + '1234 Main Street' + ',' + 'NY' + ','
+ cast(95825 as varchar(10))
This is how your query would look:
select name + char(13) + [address] + ',' + [state] + ',' + cast([zip] as varchar(10))
from tbl1 join tbl2 on....

Query display names

I have a 2 columns in a SQL Server table.
One is characters like 'Attn: firstname lastname', and the other column has a number associated with that person.
I can't figure out how to query them so the info comes out as:
Lastname, Firstname - number
If you are in SQL Server you can do this
Select Col1 + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
If you mean you need to drop the "attn:" you can do something like this
Select substring(Col1, 5,len(Col1)-5) + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
Instead of hardcoding the index of ':', you can do something like this
SELECT REPLACE(RIGHT(Col1, (LEN(Col1) - CHARINDEX(':', Col1)-1)),' ',', ') + ' - ' + Col2
To test this you can run the following
DECLARE #column1string AS VARCHAR(30), #column2number AS VARCHAR(10)
SET #column1string = 'Attn: Firstname Lastname'
SET #column2number = '12345678'
SELECT REPLACE(RIGHT(#column1string, (LEN(#column1string) - CHARINDEX(':', #column1string)-1)),' ',', ') + ' - ' + #column2number

SQL SELECT multiple columns into one

I have this query in SQL Server 2008:
SELECT Id, Year, Manufacturer, Model
FROM Table
and I need something like this...
SELECT Id, (Year + [space] + Manufacturer + [space] + Model) AS MyColumn
FROM Table
How can I get this result?
I think all integer or numeric data types you need convert to String data type. When you can create your new column.
Query:
SELECT Id, (Cast([Year] as varchar(4)) + ' ' + Manufacturer + ' ' + Model) AS MyColumn
FROM Tablename
just use ' '
SELECT Id, ([Year] + ' ' + Manufacturer + ' ' + Model) AS MyColumn
FROM Tablename

Update column with concatenated date (SQL Server)

I'm trying to update a column with the concatenated and converted results of two other columns in order to create a column with a date field. The SELECT statement returns the values I want to Update with, but I'm missing something (probably simple) on the update. It won't execute because
"the subquery returns more than one value".
However, I don't want to update with the same value for each row, but rather the concatenated result for each row.
What am I missing?
UPDATE myTable
SET myDate =
(
SELECT
CONVERT (Date,(CONVERT (NVarchar, CreatedYear) + '-' + CONVERT (NVarchar, CreatedMonth) + '-' + '01') ,0)
FROM myTable
)
I believe you have an extra SELECT that is not required. Please try:
UPDATE myTable
SET myDate =
CONVERT (Date,(CONVERT (NVarchar, CreatedYear) + '-' + CONVERT (NVarchar, CreatedMonth) + '-' + '01') ,0)
FROM myTable
This is a bit more readable in my opinion:
UPDATE dbo.tblControlMaster SET AuditAddDate = CAST(CreatedYear AS NVARCHAR(4)) + '-' + CAST(CreatedMonth AS NVARCHAR(2)) + '-' + '01'