How can I convert a date like 'Jan 1 2014 12:00AM' to 20140101?
when the month is less then 10 I need to put a zero.
I tryed with datepart but had problem adding '0' before it.
thank's.
SELECT YEAR(DateField) * 10000 + MONTH(DateField) * 100 + DAY(DateField)
To convert it to an integer. Or
SELECT convert(char(8), DateField, 112)
To convert it to a yyyymmdd string.
If your date is currently a string, you need to convert it to a real datetime value first:
SELECT CONVERT(datetime, 'Jan 1 2014 12:00AM')
(mon dd yyyy hh:miAM (or PM) is the SQL Server default format for datetime conversions, so there is no need for a format specifier here.)
If it's already a datetime, you can simply use formatting option 112, which represents YYYYMMDD:
SELECT CONVERT(nvarchar(8), myDateTimeField, 112)
(Nesting the two statements as required or converting the string result to a numeric data type is left as an exercise to the reader.)
Related
I'm trying to convert a value in my table to a timestamp but the value in the table is as below
There's a character 'T' after the date in the string.
GMT_TIME
20210608T111722.837 GMT
1st Goal:
Achieve the result in the below format
Output :
GMT_TIME
2021/06/08 11:17:22:837
2nd Goal: Trying to convert it into EST time
Output
2021/06/08 07:17:22:837
'''
select DATE( DATE_SUB( '1st goal output') , INTERVAL 4 HOUR ) ) from table1
'''
Please give me your valuable input on this.
Thank you
SELECT CAST(SUBSTRING_INDEX(REPLACE('20210608T111722.837 GMT', 'T', ''), ' ', 1) AS DATETIME(3))
Output: 2021-06-08 11:17:22.837.
For to convert from GMT to EST use CONVERT_TZ() function. Remember - you must import timezones into your server system database for to use mnemonic TZ names (not needed if you use time-formatted offsets).
If you want to substract 4 hours unconditionally then
SELECT SUBSTRING_INDEX(REPLACE('20210608T111722.837 GMT', 'T', ''), ' ', 1) - INTERVAL 4 HOUR
In this case the output datatype will be DATETIME(6), if you need strictly DATETIME(3) then use explicit CAST() additionally.
Im a bit trap in this date format.
I have a date format in my database "j-M-Y h:i a" = "10/15/2020 Thu 09:29:35 am"
i am trying to convert this date to mm-dd-yyyy format so i can get the year and make a trap sql code to select only 2022 dates in my database.
LIKE THIS QUERY
SELECT * FROM document WHERE date_received >= 2022
This code not work for me
SELECT * FROM document WHERE YEAR(date_received) >= 2022
i also tried other conversion query but i only getting "null" or invalid syntax
First, you have to cast your field as date. First trim that field. Then using date_format as '%Y", you can extract four digit year from it. If you want two digit year, you have to use '%y'.
SELECT * FROM document WHERE DATE_FORMAT(CAST(TRIM(date_received) AS DATE),'%Y') >= '2022';
If you are using varchar field for datetime values you have to convert string value to datetime using STR_TO_DATE function after that YEAR function will work becase YEAR function take date field or datetime field as input:
In your case like this:
SELECT *
FROM document
WHERE YEAR(STR_TO_DATE("10/15/2020 Thu 09:29:35 am", '%m/%d/%Y')) >= 2022
SELECT *
FROM document
WHERE YEAR(STR_TO_DATE(date_received, '%m/%d/%Y')) >= 2022
If your column data type is string "17-Aug-2020" in that case you need to convert string to date and then cast in expected date format then using %Y you can get year from your string.
SELECT * FROM document m WHERE DATE_FORMAT(CAST(TRIM(STR_TO_DATE(date_received ,'%d-%M-%Y')) AS DATE),'%Y') >= '2022';
try this example also
SELECT DATE_FORMAT(CAST(TRIM(STR_TO_DATE('May-01-2022','%M-%d-%Y')) AS DATE),'%Y')
I need to (if its possible) convert a column that stores the date as a string in the following format Monday October 19, 2015 to an usable datetime for example 01/10/2015
Every cast and convert I have tried to run it through just returns
Conversion failed when converting date and/or time from character string.
Nothing I can do about the saved format as its part of a fixed process.
Chop off the day:
DECLARE #S VARCHAR(32) = 'Monday October 19, 2015'
SELECT CAST(SUBSTRING(#S, CHARINDEX(' ', #S) + 1, LEN(#S)) AS DATETIME)
> 2015-10-19 00:00:00.000
You should manipulate the string to this format - "DD/MON/YYYY", where DD is day, MON is the first three letters of the months name, and YYYY is the year.
You could do that easily do that using string functions, and from there you can insert the date into the DB using conventional conversions, such as the ones listed in this post - Convert DD-Mon-YYYY to DD/MM/YYYY.
I have a date that is stored in 'MM/DD/YYYY' (101) format and I would like to convert it to 'DD/MM/YYYY' (103). The source date (date in 101 format) is stored as a varchar initially, so I am guessing you would first need to convert it to a date type before changing it's format.
So practically my question is, "how do you change the format of a date stored as a varchar?".
try this...
select CONVERT(varchar(10), CONVERT(datetime, '12/31/2011', 101), 103)
CAST and CONVERT are your friends here. Take a look at the date format codes in particular.
if you want to have a value of type DATE, you don't need to wrap the entire conversion with another CAST. you can just convert/cast it directly like
select CONVERT(date, CONVERT(datetime, '12/31/2011', 101))
or
select CONVERT(date, '12/31/2011', 101)
I used Leon Tayson's answer to fix a field with mixed date formats, using codes from this location:
https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
/**MM-DD-YYYY to YYYY-MM-DD **/
UPDATE [DBO].[TABLE]
SET [DATEFIELD] = CONVERT(varchar(10), CONVERT(datetime, DATEFIELD, 110), 23)
where DATEFIELD like '__-__-____'
/** excel date serial number to YYYY-MM-DD **/
UPDATE [DBO].[TABLE]
SET [DATEFIELD] = dateadd(DAY, convert(bigint, DATEFIELD) , convert(date, '1-1-1900'))
Where Len(UPDATED_DATE) = 5
/** YYYY to YYYY-MM-DD **/
UPDATE [DBO].[TABLE]
SET [DATEFIELD] = DATEFIELD + '-01-01'
Where Len(DATEFIELD) = 4
I have a problem: I have a datetime and I need the date to specific format
So I just casted datetime to time
SELECT CAST (GETDATE() AS DATE) -- result (2011-06-08)
and for formatting I use convert
SELECT CONVERT(DATE, CAST (GETDATE() AS DATE), 105) --result (2011-06-08)
105 format (dd-mm-yy)
but, the result of both is same,
CONVERT is not working for 105 formatting,
Any ideas?
thanks
To get the results you're looking for you need to convert the DATE to a VARCHAR like this:
SELECT CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105)
If you cast to a DATE, you will always get the full DATE.
You can truncate the date by re-casting to the DATE type.
SELECT CAST(CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105) as DATE)
You can use as
SELECT Convert(varchar, getdate(), 105)
select convert(varchar(50),date,105) as Date