How can I change the format of the dates produced when using ALTER COLUMN to convert a datetime column to varchar?
Tag : sql , By : Matthew Steed
Date : March 29 2020, 07:55 AM
I hope this helps . Bad, Bad idea...never ever store dates in varchar columns, now you will get garbage in there in different formats Also why varchar(16) when you want yyyyMMdd? if you want the output to be in a different format do it in the presentation layer or use convert SELECT CONVERT(CHAR(8),GETDATE(),112)
UPDATE Table
SET LastUpdateDate = CONVERT(CHAR(8),(CONVERT(DATETIME,CONVERT(varchar,LastUpdateDate))),112)
|
conversion of a varchar datatype to a datetime datatype resulted in an out-of-range value
Date : March 29 2020, 07:55 AM
Hope that helps Type 103 requires that you have datetimes with the European date/month order: 'dd/mm/yyyy' If you store month first, this may result in this error (say for '01/13/2012')
|
What column size(varchar datatype) is needed to store an image in STRING format?
Date : March 29 2020, 07:55 AM
Any of those help AFAIK sqlite uses charset conversation for TEXT columns, consider using BLOB type to store image.
|
how to change the datatype varchar(50) to datetime datatype in a table
Date : March 29 2020, 07:55 AM
|
I want to change datatype of a column [varchar] to [datetime] in sql without losing the data
Date : March 29 2020, 07:55 AM
it helps some times Create TEMP table to copy values from your table, after It copy values from table to temp table. After It you can truncate your table or column with dates, after It you can change datatype of nvarchar column to datetime, after It copy values from temp table to normal table, You can do something like: -- Creation of sample table
CREATE TABLE ChangeColType
(
Data NVARCHAR(20)
)
-- Inserting sample data
INSERT INTO ChangeColType VALUES ('10:00 AM')
-- Creatig temp table
CREATE TABLE #TempValues
(
Data NVARCHAR(50)
)
-- Copying values from table to temp table
INSERT INTO #TempValues
SELECT Data
FROM ChangeColType
-- Truncating your table
TRUNCATE TABLE ChangeColType
-- Changing datatype of your column
ALTER TABLE ChangeColType
ALTER COLUMN Data DATETIME
-- Copying back values from temp table to your table
INSERT INTO ChangeColType
SELECT Data
FROM #TempValues
-- Selecting values from table after datatype changed
SELECT CONVERT(varchar(15),CAST(Data AS TIME),100)
FROM ChangeColType
-- Dropping temp table
DROP TABLE #TempValues
|