hi
i am trying to convert a VarChar date field (e.g. 20100320) to a real date field like
'dd/mm/yyyy' (e.g. 20/03/2010).
I have tried two ways:
a)
(SELECT MIN(CAST(A.DateOfAction AS Date)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
b)
(SELECT MIN(CONVERT(DATE, A.DateOfAction, 103)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
both producing the result like
yyyy-mm-dd (e.g. 2010-03-20)
but i want the result like
dd/mm/yyyy (e.g. 20/03/2010)
any help will be appreciated.
thanks.
Try this:
select convert(varchar(8), convert(datetime, min(a.DateOfAction), 112), 103)
Your problem is that once you have a date format, SQL Server will dump it out in its default date format, which you've discovered is yyyy-mm-dd. You need to convert from date to varchar to get the format you want. But to convert from date, you need to first convert to date! 112 is the format for yyyymmdd, and 103 is the format for dd/mm/yyyy, so this is why you need these formats. (Books Online reference for date formats)
Declare #date nvarchar(100)
set #date = '20100320'
select convert(varchar, CONVERT(datetime, #date, 109), 103)
You can use
convert(varchar, CONVERT(datetime, A.DateOfAction, 109), 103)
Related
I am trying to convert/select the nvarchar datatype to date format (YYYY-MM-DD).
The table contains the date in DD/MM/YYYY format & also the null values.
Below SQL query is working fine but it has timestamp in the output
select Date4 = Convert(datetime, Last_Paid_Date, 103) FROM table
2021-01-30 00:00:00.000
My requirement is to have only the date in (YYYY-MM-DD) format
normally this should work
select Convert(date, Last_Paid_Date, 103) from tablename
But if you get conversion errors you can try this
SELECT convert(date, convert(datetime, Last_Paid_Date, 103)) FROM TableName
if Date cannot be used to convert from your format, the trick is to convert to a datetime first, and then convert that into a date.
Much much better would be to store the data in a column with type Date instead of varchar off course
I find this also some good reading
EDIT
if you keep getting conversion errors, then probably there are invalid dates in your varchar column. That is why you should never never never store dates/time in a varchar column.
To fix this, you could use this
SELECT try_Convert(date, Last_Paid_Date, 103) from tablename
this will put NULL in all columns that have an invalid date/time.
Drawback is that from all the rows that will have a value NULL, you cannot know if the original value was also NULL or an invalid date/time value.
Please try the below.
SELECT Date4 = CONVERT(DATE, Last_Paid_Date, 103) FROM TableName
OR
SELECT Date4 = CAST(GETDATE() AS DATE) FROM TableName
This will remove the Timestamp and give you only the Date values in the (YYYY-MM-DD) format.
You can go for simple conversion.
SELECT Convert(date, '20/01/2020', 103)
2020-01-20
You can go for conversion for the table as given below:
SELECT Convert(date, val, 103) as dateval FROM
(
values
('20/01/2020'),(null)
) as t(val)
dateval
2020-01-20
NULL
The issue with your query is that the column: "Last_Paid_Date" contains NULL String, which needs the conversion as they are characters.
You can try the below query:
SELECT convert(date, REPLACE(Last_Paid_Date,'NULL','01/01/2001'), 103)
, convert(datetime, REPLACE(Last_Paid_Date,'NULL','01/01/2001'), 103)
FROM table
The query will replace the NULL strings with a default value if any and then do the date/datetime conversions accordingly
You can chain two conversions : the first one converts the original dd/mm/yyyy (103) to a datetime value, and the second conversion turns that datetime into a yyyy-mm-dd (120) string.
select Date4 = convert(varchar(10), convert(date, Last_Paid_Date, 103), 120)
from table
I have saved my date as a nvarchar(50) datatype in SQL Server. When I run this query:
select [Client_code], Date_of_receipt
from [T_Receving]
I am getting output like this:
but I want to filter my records by particular date, so I wrote a query like this
select
convert(date, [Date_of_receipt], 103) as 'Date_of_Receipt'
from
[T_Receving]
where
convert(date, [Date_of_receipt], 103) between '2015-03-06' and '2018-05-06'
but its showing an error
Conversion failed when converting date and/or time from character string
You have to convert to datetime and then convert back to varchar
declare #dtv varchar(20) = '2018-17-04'
declare #dtvdt datetime = convert(datetime, #dtv, 103)
select #dtvdt;
select convert(varchar(20), convert(datetime, #dtv, 103), 103), #dtv
where convert(datetime, #dtv, 103) between '2015-03-06' and '2018-05-06'
You clearly have some bad data. Use try_convert():
select try_convert(DATE, Date_of_receipt, 103) as Date_of_Receipt
from T_Receving
where try_convert(DATE, Date_of_receipt, 103) between '2015-03-06' and '2018-05-06' ;
In SQL Server 2008, you have to work harder to find the culprits. You can start with:
select date_of_receipt
from T_Receving
where date_of_receipt not like '[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]'
This will find most instances of bad formats. If it still persists, you will have to dig deeper to find bad day or month numbers.
I'm collecting data between two date 01/12/2014 and 31/12/2014 but my sql data type in nvarchar
is my query right?
SELECT * from customer where date >= convert(datetime, '01/12/2014', 105)
AND date <= convert(datetime, '31/12/2014', 105)
Result
Msg 242, Level 16, State 3, Line 1
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
can any one solve this problem...
as I know you must separate different parts of a DATE with "-" not with "/" in format 105. here is an example:
SELECT convert(datetime, '23-10-2016', 105) -- dd-mm-yyyy
so you must rewrite your code as:
SELECT * from customer where date >= convert(datetime, '01-12-2014', 105)
AND date <= convert(datetime, '31-12-2014', 105)
The format your string are in, 'dd/mm/yyyy' is 103, not 105 (which is 'dd-mm-yyyy'). So, simply use the correct format:
SELECT *
FROM customer
WHERE [date] >= CONVERT(datetime, '01/12/2014', 103)
AND [date] <= CONVERT(datetime, '31/12/2014', 103)
If your date type is nvarchar why don't you try like this:
SELECT * FROM customer
WHERE date >= '01/12/2014'
AND date <= '31/12/2014'
Do we really need to convert?
SELECT * FROM DBO.CUSTOMER
WHERE CAST([date] AS DATE) >= '01/12/2014' AND
CAST([date] AS DATE) <= '31/12/2014'
I suggest you to use this:
(I think converting to varchar is make more sense)
SELECT *
FROM customer
WHERE CONVERT(varchar, [date], 103) BETWEEN '01/12/2014' AND '31/12/2014'
In Date and Time Styles; 103 is for with British/French standard with century (yyyy) like this dd/mm/yyyy.
Date formats are different can any one help me with different date formats
how to find datediff by using date format as ARRDAT(20140523) and DEPDAT(20140815)
The date format of the 2 values looks like yyyyMMdd which is the ISO date format.
Knowing this we can use the following:
SELECT DATEDIFF(day, ArrivalDate, DepartureDate) AS DiffInDays
FROM (
SELECT CONVERT(DATETIME, CAST(ARRDAT AS VARCHAR(8)), 112) AS ArrivalDate,
CONVERT(DATETIME, CAST(DEPDAT AS VARCHAR(8)), 112) AS DepartureDate
) AS t
CAST(expression AS VARCHAR(8)) changes the BIGINT to a VARCHAR for the CONVERT function to work.
CONVERT(DATETIME, expression, 112) specifies the format of the data is yyyyMMdd.
Try Following query:
SELECT DATEDIFF( DAY,
CAST(SUBSTRING(APPDAT,1,4)+'-'+SUBSTRING(APPDAT,5,2)+'-'+SUBSTRING(APPDAT,7,2) AS DATE),
CAST(SUBSTRING(DEPDAT,1,4)+'-'+SUBSTRING(DEPDAT,5,2)+'-'+SUBSTRING(DEPDAT,7,2) AS DATE))
FROM YourTable
My table Name is 'information'
Structure is:
No int,
Name varchar(200),
Date varchar(30)
Records:
No Name Date
=====================================
1 A 25/08/2012
2 B 10/08/2012
3 C 11/08/2012
4 D 01/09/2012
My Problem is i want the No,Name,Date for between 25/08/2012 to 01/09/2012
i was tried the follwing query, but i am unable to get it.
select No,Name,Date
from information
where Date>='25/08/2012' and Date<='01/09/2012'
I don't want to alter the date column(varchar) to datetime
Please help me
Please use the below query you do not need to alter your Date column(varchar) to datetime.
select No,Name,Date from information where CONVERT(date,[DATE],103) >= CONVERT(date,'25/08/2012',103) and CONVERT(date,[DATE],103) <= CONVERT(date,'01/09/2012',103)
As said before, convert your column to a datetime. I'd personally use a between statement as its cleaner.
select No,Name,Date from information where convert(datetime, Date, 103) between convert(datetime, '25/08/2012', 103) and convert(datetime, '01/09/2012', 103)
depending on your input parameters you might be able to convert them into datetime beforehand (so you can check the values entered before they are executed. Example;
declare #startdate datetime
declare #enddate datetime
set #startdate = convert(datetime, '25/08/2012', 103)
set #enddate = convert(datetime, '01/09/2012', 103)
select No,Name,Date from information where convert(datetime, Date, 103) between #stardate and #enddate
You can try to convert varchar to datetime:
SELECT No,Name,Date from information
WHERE CONVERT(datetime, Date, 103) >= CONVERT(datetime, '25/08/2012', 103)
AND CONVERT(datetime, Date, 103) <= CONVERT(datetime, '01/09/2012', 103)
You need to convert VARCHAR datatype to Date. Try,
SELECT No, Name, [Date]
FROM information
where CAST([Date] AS Date) BETWEEN CAST('2012-08-25' AS DATE) AND
CAST('2012-09-09' AS DATE)