I'm trying to convert an integer field to an actual date field. Someone created a "date" field that just sticks in a "date" that is actually an integer. I'm trying to convert it to an actual date.
I have tried the following to no avail:
CAST(CAST(last_purch_date AS CHAR) AS DATE) as Create,
CAST( last_purch_date as datetime) as Created,
convert(datetime,last_purch_date) as Created1,
ISDATE(CONVERT(CHAR(8),last_purch_date)) as PleaseDearGodWORK
Simple cast as date could work
Select cast(cast(20161011 as varchar(8)) as date)
Returns
2016-10-11
If your data is suspect, you could also use Try_Convert()
Select Try_Convert(date,cast(2610 as varchar(8)))
Returns
NULL
SELECT convert(date,CONVERT(varchar(8),[columname],101))
Related
I am trying to perform date conversion of a column from YYYY-MM-DD to YYYYMMDD and insert the final value into a big query partitioned table.
I am getting error as "invalid value" after conversion.
I tried the below,
CASE WHEN o_date is not null THEN PARSE_DATE("%Y%m%d",cast(a.o_date as string))
ELSE PARSE_DATE("%Y%m%d",'19000101')
END as o_date
or
CASE WHEN o_date is not null THEN cast(FORMAT_DATE("%Y%m%d",o_date) as DATE)
ELSE PARSE_DATE("%Y%m%d",'19000101')
END as o_date
Could you please help to achieve the same
Cheers!
Instead of using PARSE_DATE you can use FORMAT_DATE. According to the documentation it formats a date_expression according to a format string, as follows:
FORMAT_DATE(format_string, date_expr)
Notice that the format_string must be a STRING and it should be written between double quotes following these formatting elements dictionary and the date_expr must be a DATE.
I ran the query below to exemplify how it should be:
WITH
a AS (
SELECT
"2020-01-31" AS date )
SELECT
FORMAT_DATE("%Y%m%d", CAST(date AS DATE)) AS new_date
FROM
a
I hope it helps.
I have a table which stores date in '2019-03-28 23:59:03.000000000' this format as VARCHAR in SQL.
I want to convert it from varchar to datetime and remove the last 000000000 values from date.
From '2019-03-28 23:59:03.000000000' to this '2019-03-28 23:59:03'.
Please help me out.
Thanks
If you don't want to show millisecs, try this one. Datetime2 let's you specify how many milliseconds you want displayed, in this case I used 0.
SELECT CONVERT (DATETIME2(0), '2019-03-28 23:59:03.000000000', 121)
Simply
SELECT CAST(REPLACE('2019-03-28 23:59:03.000000000', '.000000000', '') AS DATETIME)
when you select from your table you will
SELECT CAST(REPLACE(<YourColumn>, '.000000000', '') AS DATETIME)
FROM <YourTable>;
The REPLACE() function will remove '.000000000' from your string, and remains just '2019-03-28 23:59:03' which you will cast it as DATETIME
I have the following datetime format ( as varchar ) in my database 13-04-2018 1:05:00.
I need to convert it to the following format: 2018-04-13 01:05:00. As datetime.
Normal convert functions can't do this because they try to take the 13th month, and that month doesn't exist. This error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Does someone know how to convert this date issue?
Using datetimes is always a pain regardless of the language because of all the different formats across the world.
To sort your issue out currently, you need to use a format style which is a third parameter to CONVERT. Personally what I would suggest here is to store as a datetime, because storing datetimes as strings is never a good idea. It just gets too messy later on, but if saved in the format you would like, it would be saved as yyyy-MM-dd hh:mm:ss
SELECT CONVERT(DATETIME, '13-04-2018 1:05:00',103)
You can create your own function to format it in your desired output string.
CREATE FUNCTION FormatMyDate
(#Date DATETIME) RETURNS VARCHAR(20)
AS
BEGIN
RETURN FORMAT(#Date,'yyyy-dd-MM hh:mm:ss')
END
And then you can call it in SELECT statements like this:
SELECT dbo.FormatMyDate(yourDateCol)
FROM yourTable
this takes the date from the format where month comes before day and reverses the 2 values (month and day)
SELECT CONVERT(DATETIME, '2018-13-04 01:05:00', 103);
Results:
2018-04-13 01:05:00.000
This should work for you requirement...
SELECT FORMAT(yourdate, 'yyyy-dd-MM')
Your Solution Bro...
DECLARE #d DATETIME = GETDATE()
SELECT FORMAT ( #d, 'MM-dd-yyyy hh:mm:ss', 'de-de' ) AS 'Hoping your result'
I have VARCHAR column (MyValue) in my table. It has date value in two different format.
MyValue
----------
25-10-2016
2016-10-13
I would like to show them in DATE format.
I wrote query like below:
SELECT CONVERT(date, MyValue, 105) FROM MyTable
SELECT CAST(MyValue as date) FROM MyTable
Both are giving me this error. Conversion failed when converting date and/or time from character string.
Is there anyway convert to DATE datatype format even the value stored in different formats like above?
Expecting your answers. Thanks in advance.
Does this help?
declare #varchardates table
(
vcdate varchar(20)
)
INSERT INTO #varchardates VALUES
('25-10-2016'),
('2016-10-13')
SELECT CONVERT(date,vcdate, case when SUBSTRING(vcdate, 3, 1) = '-'
THEN 105 ELSE 126 END) as mydate
FROM #varchardates
Depending on how many different formats you have in your data, you may need to extend the case statement!
See here for list of the different format numbers
You can use TRY_CONVERT and COALESCE. TRY_CONVERT returns NULL if the conversion fails, COALESCE returns the first NOT NULL value:
SELECT COALESCE(TRY_CONVERT(DATETIME, x, 105), TRY_CONVERT(DATETIME, x, 120))
FROM (VALUES('25-10-2016'), ('2016-10-13')) a(x)
I assumed the value 2016-10-13 is in format yyyy-MM-dd.
You mention in a comment you may have other formats as well. In that case it gets very tricky. If you get a value 01-12-2017 and you have no idea about the format, there is no way to tell whether this is a date in januari or in december.
I want to find records from my database which lie between any user input date range(say between 10/2/2008 to 26/9/2024). I tried using
SELECT NAME
,TYPE
,COMP_NAME
,BATCH_NO
,SHELF
,MFG_DATE
,EXP_DATE
,QTY
,VAT
,MRP
FROM STOCK_LOCAL
WHERE
convert(VARCHAR(20), EXP_DATE, 103)
BETWEEN convert(VARCHAR(20), #MEDICINEEXP_DATE, 103)
AND convert(VARCHAR(20), #MEDICINEEXPDATE, 103)
but with this query i need to enter perfect date range which is available in my database, it is not giving me data lying in between any date entered.
Thanks in advance
Since it is a poolr designed schema there isnt going to be any decent/Efficient solution for this.
In sql server if you are storing Date or Date & Time data. Use the Data or DATETIME datatypes for your columns.
In your case you are trying to compare a string with passed date. and even when you tried to convert the string (Date) into date datatype you didnt do it correctly.
My suggestion would be Add new columns to your table with Date datatype and update these columns with existing date/string values.
For now you can convert the Date(string) into date datatype using the following code.
DECLARE #MEDICINEEXP_DATE DATE = 'SomeValue1'
DECLARE #MEDICINEEXPDATE DATE = 'SomeValue1'
SELECT query....
FROM TableName
WHERE
CAST(
RIGHT(EXP_DATE, 4)
+SUBSTRING(EXP_DATE,CHARINDEX('/',EXP_DATE)+1,2)
+LEFT(EXP_DATE,2)
AS DATE) >= #MEDICINEEXP_DATE
AND CAST(
RIGHT(EXP_DATE, 4)
+SUBSTRING(EXP_DATE,CHARINDEX('/',EXP_DATE)+1,2)
+LEFT(EXP_DATE,2)
AS DATE) <= #MEDICINEEXPDATE
Note
This solution will get you the expected results but very inefficient method. It will not make use of any indexses on your EXP_DATE Column even if you have a very buffed up index on that column.