regarding ms sql data type - sql-server

I got a column in which I wanna store date formats that is dd/mm/yy, mm/dd/yy , etc. What data type do I need to use for this column in my table ? DateTime??
oh and also if I want to store Time format like 24.00 hrs..What data type then ?
I need to store the FORMATS like mm/dd/yy or dd/mm/yy not dates like 2010-12-01 or whatever..SO I should use DATeTime only ?

Store the dates as datetime or timestamp or any of the built-in date or date/time datatypes. Don't worry about formatting the value in the db itself. You can format the date using the language of your choice (whatever language you're using to retrieve the information).

If you want to store date and time you need DateTime column type.
To convert it to desired format you can use CAST and CONVERT functions.
To store formats as strings you can use VARCHAR - 'mm/dd/yy'.

Related

ADF - Change the date format from any format coming from csv to yyyy-MM-dd HH:mm:ss while loading in target sql table taking datetime

I have a situation where I am getting dates in two separate formats, MM/dd/yyyy & yyyy-dd-MM, AND there might be even more different formats as well in csv which will be obviously in string.
Below are the data which currently come as String from CSV-
1/14/2022 0:00
2021-12-31 00:00:00
I am using a Dataflow task in ADF to load the data into Azure SQL where the default format it uses should be yyyy-MM-dd HH:mm:ss.
how can I do this?
ok, i managed to build a quick demo.
Main idea of my solution:
you need to differentiate between valid rows and rows that needs to be modified.
in order to do so, i used case condition.
the idea is to add a derived column with a name 'Date' and modify only needed rows.
Input Data:
i created a csv file and saved my data as a dataset in ADF.
ADF:
In source, i select my dataset as an input.
in a derived column activity:
added a new derived column with a name 'Date' , value :
case(contains(split(Date,''),#item=='/'), toString(toTimestamp(Date,'MM/dd/yyyy H:mm'),'yyyy-MM-dd HH:mm:SS'), Date)
in toTimestamp method, i added first the dateFormat of my input Date and in toString the desired format that i want to cast the date to it.
Output:
P.s
You can cast all possible date formats that will appear in your data in that way.
you can read more about it here:
https://learn.microsoft.com/en-us/azure/data-factory/data-flow-expressions-usage#toTimestamp

TSQL Translation into Snowflake

I have SQL which I run against the on premises database. In the WHERE clause I am narrowing it down to year
WHERE YEAR(SRCSYS_ADD_DATE_TIME)=2020
and I get the results
In Snowflake work sheet I am using
WHERE EXTRACT(YEAR FROM TO_DATE(SRCSYS_ADD_DATE_TIME))=2020
and I am getting this message
Date '2020-05-28-20.42.09.724117' is not recognized'
Please need help.
Snowflake's automatic date-time parsing isn't recognizing your period separated timestamp formatting.
You'll need to explicitly specify a format to read the string during conversion:
EXTRACT(
YEAR FROM
TO_DATE('2020-05-28-20.42.09.724117', 'YYYY-MM-DD-HH24.MI.SS.FF')
) = 2020
bkan.
The problem is the timestamp format. If you don't specify a timestamp format, Snowflake will try to autodetect. In this case it can't, so you'll have to specify the timestamp format:
select year(to_timestamp('2020-05-28-20.42.09.724117', 'YYYY-MM-DD-HH24.MI.SS.FF6'));
If you don't want to specify the timestamp format, your best option is using YYYY-MM-DD HH24:MM:SS.FFFFF (or some other precision of fractional seconds) like this:
select year('2020-05-28 20:42:09.724117'::timestamp);
The second option reformats the timestamp to the standard format, and the ::timestamp syntax casts it to a timestamp.

Month and date position got interchanged after using Data conversion

I am trying to load some data from a .csv file into my SQL Server. There is a column for date which has datatype Unicode (WSTR) in the .csv file and the column for storing that date in SQL Server is of Datetime data type.
When I used DATA CONVERSION transformation to convert WSTR data to DBTIMESTAMP data, it got changed but with an error that it interchanged the month and date which gives me the wrong date.
The date should be like 2019-09-03 (for 3rd Sep 2019), but I get 2019-03-09.
Please suggest what the issue is that I am facing?
Problem
This may occurs when converting a string to a date value without specifying the date format. Reffering to the SSIS data conversion transformation official documentation:
If you are converting data to a date or a datetime data type, the date in the output column is in the ISO format, although the locale preference may specify a different format.
Assume that the data is stored in the csv file with the following date format MM/dd/yyyy and the default date format in the regional settings is dd/MM/yyyy then date values will not be converted properly.
Solution
In order to specify the date format you have to use a Script Component or a Derived column.
Derived Column
You have to reorder the date part manually, you can use TOKEN() and TOKENCOUNT() function to do that, the following expression convert dd/MM/yyyy format into yyyy-MM-dd universal format:
TOKEN([DateColumn],"/",3) + "-" + RIGHT("0" + TOKEN([DateColumn],"/",2),2) + "-" +RIGHT("0" + TOKEN([DateColumn],"/",1),2)
Script Component
You can use DateTime.ParseExact() function to parse a date based on a specific format:
DateTime.ParseExact(Row.DateColumn,"MM/dd/yyyy",System.Globalization.CultureInfo.InvariantCulture");

SQL Server Date Conversion from a specific format to a different exact date format

I have a particular situation that I can't quite figure out.
I have a field in a SQL table that is saved as a DATETIME in the following format:
2016-11-26 00:00:00
This is all good-and-well, except that I need it in dd/MM/yyyy format.
I don't want to use the CONVERT(VARCHAR(10), #dateField, 103) function in SQL because I want a DATE format, not a STRING format.
I export a result DataTable to Excel using the EPPlus library and I need the date fields in the export in this exact format.
I tried:
FORMAT(#dateField, 'dd/MM/yyyy', 'en-gb') as well as casting this back to a DATE type. It still implicitly converts to VARCHAR or NVARCHAR
Casting #dateField to DATE and SMALLDATETIME
Using C# to try and convert this to a date in the correct format. DateTime.ParseExact(dataTable.Rows[i]["DateField"].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
EPPlus column formatting: workSheet.Column(19).Style.Numberformat.Format = "dd/MM/yyyy";
I'm running out of options on this conversion because none of my attempts are working for a DateTime format. Every path I've taken converts my value(s) back to a string. Any assistance would be appreciated :)
If it's a datetime column it's saved as binary representation of the date; it's only when rendered in a client application such as management studio is it rendered in some format.
So if you need it in a particular format you mean rendered as a string in that format. You can always convert back to a datetime (and should!) when saving back to the database. For example, you may want to trim the time part from a datetime before saving.

How to get DatePart out of datetime result column in SQLite

I have a column StartDateTime which has the Value 2/10/2008 6:30:12
I need only the Date part i.e 2/10/2008 to be retrieved.
I tried using strftime() and date() functions in sqlite.
This is not one of the supported date/time formats.
Store your date/times in the format 2008-10-02 06:30:12 instead.

Resources