I have DOB varchar column which contains date value. I am getting dates in mm/dd//yyyy format.
I have to change format of all these values to yyyymmdd format in DOB column. I can't use datetime datatype for DOB column as per customer’s demand.
Please suggest what can be done.
UPDATE dbo.tablename
SET DOB = CONVERT(CHAR(8), CONVERT(DATETIME, DOB, 101), 112);
Of course there is no guarantee that all of the data in there right now is valid m/d/y data. Someone could have entered d/m/y, or something that is entirely not a date at all.
You can get close by fixing any rows that come back from this query:
SELECT DOB FROM dbo.tablename WHERE ISDATE(DOB) = 0;
Of course this won't identify any rows where someone entered 09/05/2013 and meant May 9th, not September 5th.
This also won't prevent future strings from being entered that are not valid dates (or not the dates that the user who entered them expected them to represent).
Strongly consider having your customer read these posts. We have date-related data types for a reason.
Bad habits to kick : choosing the wrong data type
Bad habits to kick : mis-handling date / range queries
You should be able to do .....
SELECT CAST( YEAR(DOB_Column) AS VARCHAR(4)) +
CAST( LPAD(MONTH(DOB_Column),2,0) AS VARCHAR(2)) +
CAST( LPAD(DAY(DOB_Column),2,0) AS VARCHAR(2)) AS MyDate
FROM MyTable
My original answer was
SELECT CONVERT(VARCHAR, CONVERT(DATETIME,'12/01/2012'), 112)
but I was typing too slow. So another alternative would be, but this is not a robust solution because it relies on formatting to be the same.
DECLARE #MyDate VARCHAR(12) = '12/25/2012';
SELECT SUBSTRING(#MyDate, 7, 4) + SUBSTRING(#MyDate, 4, 2) + SUBSTRING(#MyDate, 0, 3)
Related
I have one date columns as varchar datatype which has multiple date formats. I have to convert all different formats into one date format as 'YYYY-MM-DD'.
I am trying to convert it but couldn't make it. Below are different formats available in column.
Input
8/15/2022
15-Aug-22
15/08/2022
Required Output
2022-08-15
Honestly, I think you need to take the pessimistic approach here and assume that, possibly for a lot of your data, you don't know what the value is meant to be. As I stated in the comments, if you have the value '01/12/2021' is that 1 December 2021 or 12 January 2021, how do you know, and more importantly how would SQL Server know? As such, for dates like this you don't know and therefore the value NULL is more appropriate that a guess.
Here I use 3 different formats, an implicit one, and then 2 explicit ones (MM/dd/yyyy and dd/MM/yyyy) Then I check if the MIN and MAX values match (NULL values are ignored for aggregation), and if they do return that value. If they don't then NULL, as what value the date is is ambiguous and therefore intentionally shown as an unknown value (NULL):
You can, if needed, add more styles to the below, but this should be enough for you to work with.
CREATE TABLE dbo.YourTable (ID int IDENTITY(1,1), --I assume you have a unique identifier
StringDate varchar(20));
INSERT INTO dbo.YourTable (StringDate)
VALUES('8/15/2022'), --Must be M/d/yyyy
('15-Aug-22'),
('15/08/2022'), --Must be dd/MM/yyyy
('12/01/2021'); --Could be MM/dd/yyyy or dd/MM/yyyy
GO
SELECT YT.ID,
YT.StringDate,
CASE MAX(V.SomeDate) WHEN MIN(V.SomeDate) THEN MAX(V.SomeDate) END AS DateDate
FROM dbo.YourTable YT
CROSS APPLY (VALUES(TRY_CONVERT(date,YT.StringDate)), --Implicit conversion
(TRY_CONVERT(date,YT.StringDate,101)), --US style MM/dd/yyyy
(TRY_CONVERT(date,YT.StringDate,103)))V(SomeDate) --UK style dd/MM/yyyy
GROUP BY YT.ID,
YT.StringDate;
GO
DROP TABLE dbo.YourTable;
db<>fiddle
You can use a combination of TRY_CONVERT and REPLACE function in a CASE operator to do so.
As an example :
DECLARE #T TABLE(STR_DATE VARCHAR(32));
INSERT INTO #T VALUES
('8/15/2022'),
('15-Aug-22'),
('15/08/2022');
SELECT CASE
WHEN TRY_CONVERT(DATE, STR_DATE, 101) IS NOT NULL
THEN CONVERT(DATE, STR_DATE, 101)
WHEN TRY_CONVERT(DATE, REPLACE(STR_DATE, '-', ' '), 6) IS NOT NULL
THEN CONVERT(DATE, REPLACE(STR_DATE, '-', ' '), 6)
WHEN TRY_CONVERT(DATE, STR_DATE, 103) IS NOT NULL
THEN CONVERT(DATE, STR_DATE, 103)
END
FROM #T
So I'm trying to filter dates, but for example, when I enter the code below, I get all dates after July 1, regardless of the year. In includes 2015, and 2016 items. What I am doing wrong? The original TicketDate column is Text format in our database, that's why I converted.
SELECT
EmplCode,
Comments,
EmplName,
CONVERT(NVARCHAR, CAST(TicketDate AS DATE), 1) AS TicketDate,
CreatedBy,
ActClockInTime,
ActClockOutTime,
AdjClockInTime,
AdjClockOutTime,
ROUND(TotActTime, 2) AS ActualTime,
ROUND(TotAdjTime, 2) AS AdjActualTime,
PayrollRate,
OverTime,
Holiday,
GLAcct
FROM AttendDet
WHERE
EmplCode IS NOT NULL
AND TicketDate > '07/01/17'
ORDER BY TicketDate;
If TicketDate is not a date data type, then you need to convert it to one.
Also: The only truly safe formats for date/time literals in SQL Server, at least for datetime and smalldatetime, are: YYYYMMDD and YYYY-MM-DDThh:mm:ss[.nnn] - Bad habits to kick : mis-handling date / range queries - Aaron Bertrand
select
EmplCode,
Comments,
EmplName,
convert(varchar(30), cast(TicketDate as date), 1) as TicketDate,
CreatedBy,
ActClockInTime,
ActClockOutTime,
AdjClockInTime,
AdjClockOutTime,
round(TotActTime, 2) as ActualTime,
round(TotAdjTime, 2) as AdjActualTime,
PayrollRate,
OverTime,
Holiday,
glacct
from AttendDet
where EmplCode is not null
and convert(date,TicketDate) > '20170701'
order by TicketDate;
Also, always include a size for varchar and nvarchar.
Bad habits to kick : declaring varchar without (length) - Aaron Bertrand - you should always provide a length for all varchar or nvarchar variables/parameters.
i have a table called dbo.reminder in that i'm having a column name rdate i have declared that as
NVARCHAR(50)
i want to sort the table using where condition with the present date how can i do that?
i have tried this query
SELECT rdetail,
rid,
rdate = CONVERT(VARCHAR, CONVERT(DATETIME, rdate, 103), 103)
FROM dbo.reminder
where rdate='23/01/2017'
but its not sorting please help me regarding this ?
Do not store date as NVARCHAR. Change your rate datatype to date and try this way
SELECT rdetail,rid,rdate
FROM dbo.reminder
where rdate = convert(date,'23/01/2017' ,103)
Convert the varchar date input to date and check with your rate column.
Better to pass imput in YYYYMMDD or YYYY-MM-DD format which is universal and does not require any conversion
If you cannot change the datatype then
SELECT rdetail,rid,rdate
FROM dbo.reminder
where convert(date,rdate,103) = convert(date,'23/01/2017' ,103)
I have column Terms in my table and it contains data like this:
30D, 40D, etc.
D represents days.
My question: how can I sum date in Terms column? How can I convert string to int?
Thank you in advance.
Just use REPLACE to ditch the D and CONVERT to convert the varchar to a number....
SELECT SUM(CONVERT(int, REPLACE(Terms,'D',''))) FROM TableName
Edit: Commentor is right, CAST would work too.
And I dont get all the down votes. The guy's just asking a SQL question.
Jeez... Tough crowd.
Edit2:
OK, based on comments, it seems like you would like to get a "due date" from the terms (say, TODAY + 30D or "today + 30 days"). To do that, we'd need a DATE column. OR, we can just use today's date (GETDATE())
Assume your table has a date column called ... dt
The SQL to pull dt+'30D' would require us to add 30 "days" to dt.
DATEADD will add days, and the aforementioned CONVERT+REPLACE combo will convert '30D' to just plain '30' ...
So, you end up with the following SQL:
SELECT DATEADD(day, CONVERT(int, REPLACE(Terms,'D','')), dt) FROM TableName
The 'day' tells DATEADD to add days (that seems really obvious ... now),
the CONVERT+REPLACE tells it how many days to add
AND - dt is our column name.
SO - how about just adding "30D" to TODAY? Easy. We just swap out dt with GETDATE() ...
SELECT DATEADD(day, CONVERT(int, REPLACE(Terms,'D','')), GETDATE()) FROM TableName
SELECT AVG(SALARY) - AVG(CONVERT(int, REPLACE(SALARY,'0','')))
FROM EMPLOYEES;
I have a scenario where I have an int column with the following dates for example:
20131210
20131209
What I want is, I want to convert the above to a date datatype so that I can use it with GETDATE() function.
This is my try but I am getting an error:
SELECT CONVERT(DATETIME, CONVERT(varchar(8), MyColumnName))
FROM MyTable
WHERE DATEADD(day, -2, CONVERT(DATETIME, CONVERT(CHAR(8), MyColumnName))) < GetDate()
This is the error I am getting:
Conversion failed when converting date and/or time from character string.
You have at least one bad date in your column (it could be 99999999 or 20130231 or who knows). This is what happens when you choose the wrong data type. You can identify the bad row(s) using:
SELECT MyColumnName FROM dbo.MyTable
WHERE ISDATE(CONVERT(CHAR(8), MyColumnMame)) = 0;
Then you can fix them or delete them.
Once you do that, you should fix the table. There is absolutely no upside to storing dates as integers, and a whole lot of downsides.
Once you have the date being stored in the correct format, your query is much simpler. And I highly recommend applying functions etc. to the right-hand side of the predicate as opposed to applying it to the column (this kills SQL Server's ability to make efficient use of any index on that column, which you should have if this is a common query pattern).
SELECT MyColumnName
FROM dbo.MyTable
WHERE MyColumnName < DATEADD(DAY, 2, GETDATE());
Try:
CREATE TABLE IntsToDates(
Ints INT
)
INSERT INTO IntsToDates
VALUES (20131210)
, (20131209)
SELECT CAST(CAST(Ints AS VARCHAR(12)) AS DATE)
FROM IntsToDates
I've had a similar problem where I need to convert INT to DATE and needed to cater for values of 0. This case statement did the trick for me
CASE MySourceColumn
WHEN ISDATE(CONVERT(CHAR(8),MySourceColumn)) THEN CAST('19000101' AS DATE)
ELSE CAST(CAST(MySourceColumn AS CHAR) AS DATE)
END
AS MyTargetColumn