Convert String to datetime in SQL Server 2008 - sql-server

How will I convert date in the format Sat Mar 29 00:00:00 EST 1975 to datetime in TSQL?
I get the date in this format from an old table which defined the date of birth column as NVARCHAR and stores the data as Mon Jun dd hh:mm:ss GMT yyyy format. I need to read another table which has the dob in datetime using this value.
So basically I want to convert, say Sat Mar 29 00:00:00 EST 1975 to 1975-03-29 00:00:00.000
Is there a way in T-SQL to do this conversion? I tried the CONVERT function, but I am unable to locate the correct 'style' to use.

Examining the data format, it appears to be a fixed length string.
The first portion is the day of week, which can be discarded as it isn't needed for parsing. Next you have the month and day information, which we need. After that is the time, which can be retained or discarded depending on whether you want a date or datetime as output.
Since you are looking for a date of birth, the time zone information can most likely be safely discarded.
Finally, there is the year.
If we eliminate the day of week and the time zone, sql server will parse the rest of the string with no problem.
I recommend cast(substring(#difficultTime,5,7) + substring(#difficultTime,25,4) as date), where #difficulteTime is the column name you are converting.
If you wanted to retain the time information, the following format will work cast(substring(#difficultTime,5,16) + substring(#difficultTime,25,4) as datetime)
This assumes that your strings will be of a fixed length. The first conversion shown eliminates the day of week, the time, and the time zone from the string, leaving a parseable date.
The second conversion eliminates the day of week and the time zone, leaving a parseable datetime.

Related

angularjs Date Daylightsaving issue

I am using date to display my date on html like:
{{updateDate| date: 'dd/MM/yyyy HH:mm:ss'}}
Dates are all saved in UTC. The problem is it displays the date in locale timezone but not considering Daylight saving on/off. As in for BST it always shows +1 hr from UTC.
I want it to also consider DST(daylight saving time).
Any help, please.
{{ date_expression | date : format : timezone}}
As per angular date filter documentation:
date: Here date can be Date Object, milliseconds or ISO 8601 datetime string formats (like: e.g. yyyy-MM-ddTHH:mm:ss.sssZ). Here Z is 4 digit (+sign) representation of the timezone offset (-1200-+1200). If no timezone is specified in the string input, the time is considered to be in the local timezone.
format: this is optional, If not specified, mediumDate(equivalent to 'MMM d, y' for en_US locale (e.g. Sep 3, 2010)) is used.
timezone: Timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the timezone of the browser will be used.
This may help you.

Day and month swapping only for single digits in SSIS

I have written an SSIS integration which fetches an employee and his expiry date. All the data is flowing correctly; however when a single digit day and month is present in expiry date, the destination column swaps the month and day (when the date has double unit for months or days its fine).
Example:
07/08/2016 to 2016-07-08 WRONG
15/03/2016 to 2016-03-15 CORRECT
since your date are in dd/MM/yyyy and you are in trying to convert it into datetime,so problem occurs.
I think this more safe,
declare #jk varchar(20)='07/08/2016'
select right(#jk,4)+'-'+substring(#jk,charindex('/',#jk)+1,2)+'-'+substring(#jk,1,2)

Can anyone help me identify this timestamp format?

I am importing information from an Oracle database on an AIX machine into SQL Server 2008r2. I inherited this process from the previous DBA. The timestamp comes in the following format: 4170180534, which, based on the conversion function in the executable, converts to the following:
417 = year (2017)
018 = days since beginning of year (018 converts to Jan 18)
0534 = time HH:mm
I need to provide maintenance on the conversion function (the previous DBA retired in 2016, so the date conversion function only works through the end of 2016).
Can anyone tell me exactly what this timestamp format is? I assume the '4' stands for the century, but it would be nice to know for sure what the first digit of the value actually is.
4should stand for weeks since start of year
format for that would be
(weeks since 1st jan, 2last digits of year, days since 1st jan, hours, minutes)
WW IY DDD HH MI

formatting date (saved in DB as datetime)

I have a bunch of records in SQL SERVER and i'm having an issue with one of the fields.
The datatype is datetime. The system was only inserting a date with no time in it, so '1/2/2017' - so when it was inserted in SQL SERVER it would show only as 1/2/2017 00:00:00. Now, what I'm trying to do is to display it just as it is saved in the DB.
I query the DB and display it like this...
If Not IsDBNull(dr("ReceivedOn")) Then
txtReceivedOn.Text = Format(dr("ReceivedOn"), "MM/dd/yyyy hh:mm tt")
End If
The mask on my masked textbox is liek this.....
00/00/0000 90:00 aa
When it does get displayd in the masked textbox is shows the date and 12:00 AM
1/2/2017 12:00:00 AM
Is there to get rid of it, and only show 0's instead of a incorrect time? However I'd like to only see a 12 hr time rather than 24.
You need to change the format specifier for hours from hh to HH
If Not IsDBNull(dr("ReceivedOn")) Then
txtReceivedOn.Text = Format(dr("ReceivedOn"), "MM/dd/yyyy HH:mm tt")
End If
On MSDN at the DateTime Custom Date and Time strings you can read
The "HH" custom format specifier (plus any number of additional "H"
specifiers) represents the hour as a number from 00 through 23; that
is, the hour is represented by a zero-based 24-hour clock that counts
the hours since midnight. A single-digit hour is formatted with a
leading zero.
Note that, with this format, the tt is meaningless.

Datetime field using military time - need time only in standard time

I have a datetime field in SQL Server 2008 which stores the date and time in military format (or international format if you prefer)
examples:
2011-02-15 10:00:00.000
2011-02-15 15:30:00.000
2011-02-15 17:30:00.000
I need to retrieve the time only portion of this in standard U.S. time format.
So
2011-02-15 10:00:00.000 needs to become 10:00 AM
2011-02-15 15:30:00.000 needs to become 3:30 PM
2011-02-15 17:30:00.000 needs to become 5:30 PM
I am looking for the best way to accomplish this in T-SQL please.
One way is:
Convert to varchar, which will give you:
Select CONVERT(varchar, #dt, 100) -- where #dt is your date time
Feb 15 2011 3:30PM
Subsequently, to remove the date, and get 7 chars from the right, which is the most, and TRIM is added just for extra safety, but probably isn't needed.
Select LTRIM(RIGHT(CONVERT(varchar, #dt, 100),7))
Will give you 3:30PM
Side Note: Denali makes this easier, no more magic numbers
As you requested this in T-SQL, you might want to look at the CAST and CONVERT syntax which specifically lists various date and time formats.
For example:
select convert(varchar, getdate(), 100)
Would give you:
Feb 3 2012 3:26PM
DateTime is stored in an internal representation - there is no format associated. When you need to display the DateTime, specify the formatting you want for converting the DateTime into a string.
It is much better to let the application decide on formatting than formatting in SQL.
See standard and custom Date and Time Format Strings for more information.

Resources