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.
Related
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)
I want to insert date from one table to another. But the problem is, source table is having date stored as varchar and destination as date datatype.
Also I want to change the format of the date while inserting to destination.
Table1
Date varchar(80) null
Table2
Date Date null
Date in Table1 is stored in mm-dd-yyyy
I want to convert it into dd.mm.yyy and store it into Table2.
How should I convert it?
I tried below, but its not working.
select CONVERT(VARCHAR(10),TRY_Convert(date,[Date]),104)
If your destination table has a date field of date type all you should do is to convert your source data to date type and insert it.
The format dd.mm.yyyy that you want as output is NOT date format but a string and you can always get your date data from the second table using convert to string with the format 104 while selecting data from your second table like this:
declare #Table1 table ([Date] varchar(80) null);
insert into #Table1 values ('12-31-2001'), ('02-28-2002');
declare #Table2 table ([Date] Date null);
insert into #Table2
select convert(date, [date], 101)
from #Table1;
select convert(char(10), [date], 104)
from #Table2;
Here I used table variables and you should use your Table1, Table2 instead
Assuming style 104 is what you want, you just need one convert():
select TRY_CONVERT(VARCHAR(10), [Date], 104)
Your question is confusing because you specify that the source is a varchar and the destination a date. That would imply:
select TRY_CONVERT(DATE, [Date], 104)
I would also fix the table with the varchar "date" column so it uses the date data type. That is the right way to store dates. If you are going to store them as strings (which is occasionally necessary), then use YYYY-MM-DD format. This is (almost totally) unambiguous and easily turned back into a date.
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.
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)
I want to create indexed view with such t-sql:
Select
Table1_ID,
cast(CONVERT(varchar(8),
t2.Object_CreationDate, 112)AS DateTime) as Object_CreationDate ,
Count_BIG(*) as ObjectTotalCount
from
[dbo].Table2 t2 inner join [dbo].Table1 t1 on ...
Group BY
Table1_ID, CONVERT(varchar(8), t2.Object_CreationDate, 112))
I need to make group by only by datepart of column Object_CreationDate (type datetime2 ).
Also I want to set index on columns Theme_Id AND Object_CreationDate in the derived view.
If I use cast(CONVERT(varchar(8), m.Mention_CreationDate, 112)AS DateTime) in SELECT - I'll get problems with index on this column. Because this column (Object_CreationDate) is not deterministic.
I wonder if it is possible to solve a problem.
replace ...
CONVERT(varchar(8), t2.Object_CreationDate, 112))
... with
DATEADD(day, DATEDIFF(day, 0, t2.Object_CreationDate), 0)
--OR
CAST(t2.Object_CreationDate AS date)
The 2nd format is SQL Server 2008+ only, the 1st is more general
This removes the time component from a datetime value in the date/datetime datatype domain without any intermediate locale dependent datetime formats
See these answers: One and Two(comments)