SQL Server - Exporting table from SQL Server 2008 without CAST - sql-server-2008-r2

I am on SQL Server 2008 R2 Management Studio and I was trying to export a table to SQL file as INSERT INTO... but in that table I have also a smalldatetime field which is gonna exported as CAST(0x9E7501E0 AS SmallDateTime) for example... is there a way to export that smalldatetime to .sql where smalldatetime would be represented as '2014-02-05 11:10:34' ?
thanks in advance to everyone!!
Cheers,
Luigi

You can use Convert, which has lots of data format options:
See: http://www.w3schools.com/sql/func_convert.asp or How to convert DateTime to VarChar for examples.

Use CONVERT function like
select CONVERT(smalldatetime, your_date_time_field, 120) as new_date_time
(OR)
If you want to convert it to varchar
select CONVERT(varchar, your_date_time_field, 120) as new_date_time
Here 120 is the format style which will represent the output as yy-mm-dd h:m:s

Related

how to convert "MMM-YY" from varchar to date?

Does anyone have any idea how to convert a date formatted "mmm-yy" (varchar) to "dd-mm-yyyy" or only from varchar data type to date data type.
It actually depends on what language or tool you are using, but there is almost certainly a builtin function that will help you do this.
Checkout
MySQL: DATE_FORMAT()
Oracle: SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY') FROM dual;
SQL Server: SELECT CONVERT(VARCHAR(10), GETDATE(), 120);
Or if you are doing this using a programing language i.e., Python, JavaScript etc. Just use the built in string replace methods to change the date into your desired format.

T-SQL: DateTme Column varchar cast that may contain '1753-01-01' to empty string

Task: convert a DateTme column (cast as varchar) that may contain 1753-01-01 to an empty string.
What is the most efficient way to do this in T-SQL?
For example, if I use:
SELECT
LEFT(CONVERT(VARCHAR, DateCol1, 120), 10)
FROM
ourDatabase.dbo.ourTable
How can I efficiently change any row containing '1753-01-01' to an empty string?
NOTE: it appears the value '1753-01-01' really does appear in the database table; it sometimes is returned where there are nulls. One can even do a datepart yy and get 1753, so it is NOT ‘null’ in the ordinary sense.
Please see also:
What is the significance of 1/1/1753 in SQL Server?
for background on this specific date.
Also, please see: {general discussion of Datetime to varchar conversion}:
How to convert DateTime to VarChar
This is for Microsoft SQL Server 2014 using SSMS 14.0.17
Use replace().
SELECT replace(left(convert(varchar, datecol1, 120), 10), '1753-01-01', '')
FROM ourtable;
db<>fiddle

PostgreSQL: Equivalent way to express the following Oracle SQL, HH.MI.SSXFF AM

The oracle script I'm in the process of 'converting' so it can be executed in PGAdmin4, has the following values to insert into a column of table with a data type of 'date'
to_timestamp('12-JUN-99','DD-MM-YY HH.MI.SSXFF AM')
From my understanding, FF represents Fractional Seconds.
What would be the equivalent way to represent the statement in PostgreSQL/PGAdmin 4?
SSXFF is my main concern.
I don't see how that code works in Oracle. But this should work in both Postgres and Oracle:
select to_timestamp('12-JUN-99', 'DD-MON-YY')

T-SQL date conversion dd/mm/yyyy to yyyy-mm-dd

I've read a couple of posts on converting from dd/mm/yyyy to yyyy-mm-dd but the code is not working (CONVERT AND CAST). Please see example below and kindly advise if you can help as the error I'm getting is 'Conversion failed when converting date and/or time from character string'. If I change my date_engaged to '2010-09-12' it will work but I need to convert it somehow because if i say select getdate(), it will spit out this date format (yyyy-mm-dd).
It's interesting because the code is not working for me on my work PC and I'm using Microsoft SQL server Management studio 2008, but at home, it works perfectly fine.
create table test(
name varchar (10),
date_engaged nvarchar (20),
timestamp2 nvarchar (20),
LOS nvarchar (20)
)
insert into test (name,date_engaged) values ('JJ','12/09/2010')
update test
set timestamp2=CAST(DATEADD(month,0,dateadd(month,datediff(month,0,getdate()),-1)) AS DATE)
update test
set LOS=DATEDIFF(day,date_engaged,timestamp2)/365.25
thx
You need to take a look at the MSDN docs for CONVERT where you will find that we need to provide the format also so that it can format the date as per our reruirement. The reason why it is working on your home system, is may be because your system is configured in the format in which you are giving the dates.
A fix to your problem
DECLARE #x VARCHAR(13) = '12/09/2010'
SELECT RIGHT(#x,4) + '-' + LEFT(RIGHT(#x,7),2) + '-' + LEFT(#x,2)
DEMO
On a side note:
It is not recommended to use NVARCHAR or VARCHAR or CHAR to store dates, so as to avoid these types of issues. You can better use DATE datatype to store dates.

Extract portion of date string and convert to datetime in T-SQL

I'm running SQL Server Standard 2008 R2 on a 64 bit version of Windows Server 2008 R2 standard (sp1)
I've imported a log file as a flat file source. One of the columns from the import called col2 in the table called big holds values like this: 16/Mar/2007:11:30:17 as varchar(50).
I want to convert that column (col2) to a datetime datatype.
One method I was trying was to extract each part of the date string and then recombine and convert them.
The problem I ran into is that each column has different widths since the log file couldn't be neatly delimited, making using something like CHARINDEX return a single digit or sometimes NULL.
I've been attempting to set up using regex using CLR integration but can't get it to work (I can't create a C# project in Visual Studio, there's no option for it) and Master Data Services won't install because SQL Server 2008 R2 Standard doesn't support it.
What is my best method to do this? Using CASE, SUBSTRING and CHARINDEX?
Try this
DECLARE #d varchar(50) = '16/Mar/2007:11:30:17'
SELECT CAST(STUFF(#d,CHARINDEX(':',#d),1,' ') AS DATETIME)
Though I liked the idea of EricZ, however, here is my solution
DECLARE #d varchar(50) = '16/Mar/2007:11:30:17'
SELECT CAST( LEFT(#d,PATINDEX('%:%',#d) - 1) + ' ' + SUBSTRING(#d,PATINDEX('%:%',#d) + 1,LEN(#d)) AS DATETIME)
select convert(datetime,
SUBSTRING('16/Mar/2007:11:30:17', 0, CHARINDEX(':', '16/Mar/2007:11:30:17')) + ' ' +
SUBSTRING('16/Mar/2007:11:30:17', CHARINDEX(':', '16/Mar/2007:11:30:17') + 1, 8) , 1)
try this>
select convert(datetime,STUFF(big,CHARINDEX(':',big),1,' '),101) from yourtable
Example:Check here as datatime string format.If its the same it will work
DECLARE #d varchar(50) = '16/Mar/2007:11:30:17'
select convert(datetime,STUFF(#d,CHARINDEX(':',#d),1,' '),101)