SQL Server - Add results of a SELECT query to an existing table - sql-server

I currently have a table in SQL Server called masterTable. What I'd like to do is to add a new column to this table called report_Date.
I would like to store the results of a SELECT query in the report_Date column. The query works fine (it converts a nvarchar into a desired date format). What I can't do is append the results to the column.
My query:
SELECT
CONVERT(varchar(10), CAST(FileName AS date), 103) AS FileName
FROM dbo.masterTable
Any suggestions please?

If I understand your question correctly then
UPDATE dbo.masterTable SET
report_Date = CONVERT(varchar(10), CAST(FileName as date), 103)

Related

Selected Records in SQL Server Were Placed In Wrong Columns

I have used a select statement in SQL Server which generated records with 5 columns.
As you can see in record 4, the product that had a very long data was inserted in the wrong column. Why did this happen? I am using SQL Server 2012 by the way. TIA!
Were you pasting the results into Excel? If so, I suspect a CRLF in your long text.
Consider the following
Declare #YourTable table (ID int,Descr varchar(max))
Insert into #YourTable values
(1,'Some Short Desc'),
(2,'Some Long Desc with a CRLF
Some more text on the second line'),
(3,'Some Short text')
Select * from #YourTable
In SSMS it looks like this
If pasted into excel, you will see
One way to correct this is to strip CHAR(13) and CHAR(10)
Select ID,Descr=replace(replace(Descr,char(13),''),char(10),'') from #YourTable
You have not provided the full information so I'm assuming Sql Server is showing 4 records but when you copy these records into excel then there are 5 records like the picture which you have shared.
Solution to this problem is you have to remove the new line character and line feed from the product column.
Use below query:
SELECT ID, Title, REPLACE(REPLACE(product, CHAR(13), ''), CHAR(10), ''), country, isActive from YourTableName

SQL Server : convert datetime in place in table

Basically I inherited a rather large table which has a few columns with dates formatted as varchar(10) of '##.##.####' (day.month.year). I would like to convert these columns to Datetime columns for that specific date.
Is there anyway to convert these in place on SQL Server (through a single set of SQL queries), instead of SELECTing each record in some programming language, converting the date and then UPDATEing back as Datetime (perhaps to a different column)?
There is no need in ...=(select...) The action is simple and straightforward.
ALTER TABLE YourTable
ADD NewDateColumn DATETIME
update YourTable set NewDateColumn = convert(date, OldDateColumn, 104)
--Magic 104 is for dd.MM.yyyy date format
That's all.
How about this as an alternative to Jim's comment. First add new DATE column:
ALTER TABLE YourTable
ADD NewDateColumn DATETIME
Then to UPDATE (I suppose you would want to use the same standard you've been using):
UPDATE t
SET t.NewDateColumn = (SELECT CONVERT(DATE, OldDateColumn, 104)
FROM YourTable ta WHERE t.ID = ta.ID)
FROM YourTable t
For someone that might want to get it from the German standard to the U.S. standard:
UPDATE t
SET t.NewDateColumn = (SELECT CONVERT(DATETIME,
CONVERT(DATE, OldDateColumn, 104)
,101)
FROM YourTable ta WHERE t.ID = ta.ID)
FROM YourTable t
This is assuming you do have an ID column.

Custom autogenerated ID in sql server

How can I create a custom auto-generated ID in this format:
yyyymmdd-xxxxx
where:
yyyymmdd is the present date and
xxxxx is an auto increment integer number starting from 0
and the next day xxxxx should be restarted to 0.
Thanks
Create one autonumeric ID column in your db
save the date/time of each insert row.
Then use a ROW_NUMBER() function
SELECT *,
dayField + '-' + CAST(rn AS VARCHAR(100))
FROM (
SELECT ID, dateTimeField,
-- truncate the time and convert to yyyymmdd
CONVERT(VARCHAR(10), cast(dateField As Date), 112) as dayField,
ROW_NUMBER() OVER (PARITION BY cast(dateField As Date)
ORDER BY dateTimeField) as rn
-- or just ORDER BY ID
) T
Now if you want save this on the db, you probably will need a trigger.
You might consider including an Identity column and a datetime column that will be set on insert and than add a calculated column to put the two together.

SQL Select for create daily log data from more than one date column frrom a single table

I have a table with more than one date column,
each date column hold a date or null value,
I want to write a SQL query which will display each column have date into a new row with a new additional column named LogDate that contain the same date of column.
Its difficult to explain, please referrer the attached image.
Just use UNION ALL to concatenate the three result sets:
SELECT [ReceivedDate] AS LogDate, * FROM MyTable WHERE [ReceivedDate] IS NOT NULL
UNION ALL
SELECT [Closing Date] AS LogDate, * FROM MyTable WHERE [Closing Date] IS NOT NULL
UNION ALL
SELECT [LPODate] AS LogDate, * FROM MyTable WHERE [LPODate] IS NOT NULL
To sort by LogDate simply add the following ORDER BY clause to the end of this query:
ORDER BY LogDate

how to set order by date columns in sql server

i have a table in DB and columns like
01-03-2013
01-04-2013
02-03-2013
i want show order by like
01-03-2013
02-03-2013
01-04-2013
Please help me.
Most probably your column is not stored as one of the date datatype this is being sorted as a string based on ASCII codes
You can cast your column for sorting.
SELECT * FROM tbl
ORDER BY CONVERT(datetime, userColumn, 106)
otherwise it's straight forward
SELECT * FROM tbl
ORDER BY userColumn
If your column contain invalid records as well, you can use default date for such records
SELECT * FROM tbl
ORDER BY CONVERT(datetime,
CASE ISDATE(userColumn)
WHEN 1 THEN userColumn
ELSE '01-01-1900'
END, 106)

Resources