SQL Server - Convert datetime to JSON date format - json

How can I convert SQL smalldatetime or datetime like this:
select cast(getdate() as smalldatetime)
To JSON format like this:
/Date(1576278000000+0100)/

I just find this post in another issue like you got it.
Maybe it would work for you
"render": function ( data, type, row ) {
var fecha = new Date(parseInt(data.match(/\d+/)[0]));
return fecha.getMonth() + 1 + '/' + fecha.getDate() + '/' + fecha.getFullYear();;
},
"targets": 0
and this is the link that I found
https://es.stackoverflow.com/questions/125686/convertir-fecha-en-javascript

Related

How to allow "0" value in date derived column in SSIS package

In SSIS package I am using a flat file source with a date column and some of the dates are with 0 value. The date column format is YYYYMMDD and I am converting this to YYYY-MM-DD format in derived column. If the date exists, the date is converting, but getting error for the date if it has 0. source file date datatype is DT_STR and destination SQL table field datatype is smalldatetime.
Please suggest logic for allowing the zero value in the derived column. I am using below logic.
[Date] == 0 ? NULL(DT_DATE) : (DT_DATE)(SUBSTRING([Date],1,4) + "-" + SUBSTRING([Date],5,2) + "-" + RIGHT([Date],2))
Can you try to replace (DT_Date) with (DT_DBDATE) ?
[Date] == 0 ? NULL(DT_DBDATE) : ...
You are mixing data types. [Date] is a string and can't be compared to 0.
Try this:
[Date] == "0" ? NULL(DT_DATE) : (DT_DATE) (SUBSTRING([Date],1,4) + "-" + SUBSTRING([Date],5,2) + "-" + RIGHT([Date],2))

Convert string to date format 105 in sql server

I had string in the order of ddmmyy i.e '231013' which i need to convert it into dd-mm-yy format
I tried following but it is giving yyyy-dd-mm format
I know 105 will give dd-mm-yy format
DECLARE #ITEMS AS NVARCHAR(10)='231013'
SELECT CONVERT(DATE,#ITEMS,105)
but it is returning
2023-10-13
How to convert string in the order of ddmmyy to date dd-mm-yy
Convert string in the order of ddmmyy to date dd-mm-yy
DECLARE #ITEMS AS NVARCHAR(10)='231013'
SELECT CONVERT(varchar(25),
CONVERT(DATE, SUBSTRING(#ITEMS, 1, 2) + '-' + SUBSTRING(#ITEMS, 3, 2) + '-' + SUBSTRING(#ITEMS, 5, 2), 5),5)
result will be
It seems that the mmddyy style is not natively supported by CONVERT; to perform the conversion, I would just adapt the input string to one of the supported styles, like mm-dd-yy:
SELECT
CONVERT(DATE, SUBSTRING(#ITEMS, 1, 2) + '-' + SUBSTRING(#ITEMS, 3, 2) + '-' +
SUBSTRING(#ITEMS, 5, 2), 5)
If you just need to change the format (Not converting to a DateTime type), you could Stuff() it:
Declare #Items as nvarchar(10)='231013' --ddmmyy
Select Stuff(Stuff(#Items,3,0,'-'),6,0,'-') --dd-mm-yy
Fiddle demo

SSIS Package string to datetime

I have a derived column data flow component and I need to insert data to a table.
I am having a problem converting the following string to DATETIME "20130822 14:52:53", how would I go about this?
Please assist
Derived column code:
(DT_DBTIMESTAMP)(SUBSTRING(LTRIM(string),1,4) + "-" + SUBSTRING(LTRIM(string),5,2) + "-" + SUBSTRING(LTRIM(string),7,2) + SUBSTRING(LTRIM(string),9,LEN(LTRIM(string)) - 7))
Result:
string date
20130822 14:52:53 2013-08-22 14:52:53.000

SQL Server equivalent of MySQL DATE_FORMAT()

Hope we're having a good day and all set for Christmas.
Got a quick question. I'm converting a MySQL function into SQL Server, I've got most of the function converted except for one part which has the following:
#description = CONCAT(#description, date_format(#INLastUpdated, '%H:%i %d %b %Y'))
What I'm trying to do is to basically recreate the date_format function to format the date in the same way specified, but I'm not sure how to do it. from what I've seen in the MySQL documentation the format selected would give hour:minute day / short month name / year.
Anyone got any ideas?
You can try this:
DECLARE #description VARCHAR(1000) = 'test'
DECLARE #INLastUpdated DATETIME = '2012-12-21 14:32:22'
SET #description = #description + ' '
+ LEFT(CONVERT(VARCHAR(8), #INLastUpdated, 8), 5) + ' '
+ CONVERT(VARCHAR(20), #INLastUpdated, 106)
SELECT #description
But be careful as format 106 depends on local language settings. Read more on MSDN
The equivalent function is CONVERT. But you're basically out of luck. SQL Server does not allow to cherry-pick the date tokens. You need to browse the available full date built-in formats and choose one, or try to compose an output by string concatenation, as in:
CONVERT(VARCHAR, CURRENT_TIMESTAMP, 103) + ' ' + CONVERT(VARCHAR(8), CURRENT_TIMESTAMP, 114)
Which version of SQL Server? In SQL Server 2012 we now have the FORMAT function.
This, in MySQL
date_format(#INLastUpdated, '%H:%i %d %b %Y')
translates into something like this for SQL Server 2012 using the FORMAT function:
DECLARE #d DATETIME = GETDATE();
SELECT FORMAT(#d, 'HH:mm d MMM yyyy', 'en-US') AS 'DateTime Result';
BOL SQL Server 2012 > FORMAT (Transact-SQL)

current_date casting

string selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < current_date;";
When I call current_date, it returns yyyy-MM-dd format, but I want to return dd.MM.yyyy format, how can I do that. please help. My program works fine when I am trying
string selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < '16.04.2010';";
The MySQL date format is YYYY-MM-DD, but using str_to_date() and date_format() you can change the date format.
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
In your case, try DATE_FORMAT(current_date, '%d.%M.%Y')
mysql has a function that return current date - curdate()
So, your query must be like
update table set state_mode_id=1 WHERE stoping_mode < curdate()
But you have 2 major faults (or even 3):
date field must have a format of YYYY-MM-DD, not anything else
such an update is senseless at all, because all such states being evaluated at select time
assembling table name at query time is a sign of very bad database design
YYYY-MM-DD is mySQL's native date format. I find it hard to believe that doing a comparison against a DD-MM-YYYY string works?
Anway, what you are looking for is DATE_FORMAT().
Use any function like this to return your format
function ChangeDateforShow($inputdate) {
if($inputdate>0) {
$date = substr($inputdate,8,2);
$month = substr($inputdate,5,2);
$year = substr($inputdate,0,4);
$show = $date.".".$month.".".$year;
return $show;
}
}
function ChangeDateforDB($inputdate) {
if($inputdate>0) {
$date = substr($inputdate,0,2);
$month = substr($inputdate,3,2);
$year = substr($inputdate,6,4);
$show = $year."-".$month."-".$date;
return $show;
}
}
Ok use this function in db itself like
selectSql = "update " + table + " set state_" + mode + "_id=1 WHERE stoping_" + mode + " < 'ChangeDateforDB(customDATE)';"
Or otherwise you will move for mysql date_format() only